I'm trying to loop over the following JSON, but I can't figured out how to map 开发者_JAVA百科the loop to the correct data item.
Alerting data[0].ID
etc... keeps returning undefined
, for example:
{
"COLUMNS":
["ID","NAME","USECOUNT","EXCERPT"],
"DATA":
[
[1443,"foo",20,null],
[810,"bar",10,null],
[690,"foobar",10,null]
]
}
var obj = {
"COLUMNS":
["ID","NAME","USECOUNT","EXCERPT"],
"DATA":
[
[1443,"foo",20,null],
[810,"bar",10,null],
[690,"foobar",10,null]
]
};
// assuming you are interested only in the first elements of each DATA array
$.each(obj.DATA, function(i, val) {
alert(val[0]);
});
http://jsfiddle.net/AbVme/
jsonObject.data[0][0];
there is no ID
The COLUMNS
parameter and the DATA
parameter are disjointed, they are two separate parameters.
If you want to do what you're talking about, you would need some way of determining which index "ID" resides at within the columns array.
var idIndex = getIndexOf("ID", jsonObject.COLUMNS);
var id = jsonObject.DATA[0][idIndex];
精彩评论