getting column names on a JSON object exported from .NET arrayList

764 views
Skip to first unread message

lisman...@gmail.com

unread,
Jan 17, 2008, 5:50:24 PM1/17/08
to Jayrock
I am outputing JSON to a web page from a multi-dimesional .NET
arrayList

It would help a lot if I could apply column names to ease the work on
the client side.

How do I do this?

Thanks, Aaron

Atif Aziz

unread,
Jan 17, 2008, 6:23:43 PM1/17/08
to jay...@googlegroups.com
Not sure what you mean. Could you give a concrete example of what you have in array list and how you would like to see the JSON output.

Aaron Lisman

unread,
Jan 17, 2008, 8:11:56 PM1/17/08
to jay...@googlegroups.com
I would like the JSON to come out like this:

tablename
fieldname:value
fieldname:value

so when i reference the json object, I can do something like, obj[0][3]['firstName']

I think if you export from a dataset, that's what you get. Typed.

Atif Aziz

unread,
Jan 18, 2008, 3:56:23 AM1/18/08
to jay...@googlegroups.com

If you export a DataSet, you get an object where each member is a table based on the table name. The table itself is an object with two members, namely “columns” and “rows”. The “columns” member is an array of column names whereas “rows” is an array of rows where each row is an array of values. The shape looks like this:

 

{

    "Employees" : {  

        "columns" : [  

            "EmployeeID", "LastName", "FirstName" ],

        "rows" : [

          [ "1", "Davolio", "Nancy" ],

          [ "2", "Fuller", "Andrew" ],

          [ "3", "Leverling", "Janet" ]

    }

}

 

When you export a DataTable, you get just the object with columns and rows:

 

{

    "columns" : [

        "EmployeeID", "LastName", "FirstName" ],

    "rows" : [

      [ "1", "Davolio", "Nancy" ],

      [ "2", "Fuller", "Andrew" ],

      [ "3", "Leverling", "Janet" ]

}

 

In both these cases, you can turn a table into a more accessible object on the client end. For example, when you receive the JSON representation of a DataSet on the client in JavaScript, you can convert the table rows into first-class objects by adding a handy method to the base Array object:

 

Array.prototype.toObject = function(names)

{

    var o = {} ;

    for (var i = 0; i < names.length; i++)

        o[names[i]] = i < this.length ? this[i] : null;

    return o;

}

 

Given this, you can convert a row that is an array of values into an object by supplying the column names as the first parameter:

 

var table = ds[0]; // get the first table in the dataset

var row = table.rows[0].toObject(table.columns);

 

Now you can do row["FirstName"].

 

Finally, if you export a DataRowCollection or just an array of DataRows, then they’re formatted out directly as JSON objects:

 

[

    {

        "EmployeeID" : "1",

        "LastName" : "Davolio",

        "FirstName" : "Nancy",

    },

    {

        "EmployeeID" : "2",

        "LastName" : "Fuller",

        "FirstName" : "Andrew"

    },

    {

        "EmployeeID" : "3",

        "LastName" : "Leverling",

        "FirstName" : "Janet"   

    }

]

 

HTH

Aaron Lisman

unread,
Jan 18, 2008, 10:13:54 AM1/18/08
to jay...@googlegroups.com
thanks so much!
Reply all
Reply to author
Forward
0 new messages