Reputation: 9080
I am getting this JSON string from an ASP.Net webservice:
{"d":{"Table":[{"col1":123,"col2":"name","col3":"name","col4":100,"col5":"\/Date(1153033200000)\/"},{"col1":123,"col2":"name","col3":"name","col4":101,"col5":"\/Date(1153033200000)\/"},{"col1":123,"col2":"name","col3":"name","col4":102,"col5":"\/Date(1153033200000)\/"}]}}
In my jQuery how do I reference the Table code so I can loop through the data?
msg.d[i].col1
What am I missing? msg.d.table[i]?
Upvotes: 1
Views: 365
Reputation: 61
You can use jQuery's JSON parser:
data = jQuery.parseJSON(JSON_DATA);
And then refer to objects directly via the data variable:
data.my_property
Upvotes: 0
Reputation: 700362
The property d
is an object that contains the property Table
, which is an array of objects that contain the property col1
.
So, you use msg.d.Table
to access the array, msg.d.Table[i]
to access an item in the array, and msg.d.Table[i].col1
to access the property in the item.
Note that Javascript is case sensetive, so while msg.d.Table
works, msg.d.table
won't.
This gets the array and loops through it:
var tableArray = msg.d.Table;
$.each(tableArray, function(){
alert(this.col1);
});
Upvotes: 2
Reputation: 2209
$.each(msg.d.Table, function(i, val) {
alert(val.col1);
});
I hope this helps!
Upvotes: 1
Reputation: 3689
msg.d
is an object. msg.d.Table
will give you what you want.
To iterate:
$.each(msg.d.Table, function(row) {
// Get specific value:
window.alert(row.col1);
// Iterate through all columns:
$.each(row, function(column, value) {
// Do something..
});
});
Upvotes: 2