开发者

How can I loop over this JSON result with jQuery?

开发者 https://www.devze.com 2023-03-26 12:10 出处:网络
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.

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消