1

I'm trying to loop over the following JSON, but I can't figured out how to map 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]
        ]
} 

3 Answers 3

1
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/

Sign up to request clarification or add additional context in comments.

2 Comments

That's done it. Thank you. Will accept as soon as it lets me.
Interesting. I'm running this in an ajax function, but it's craping out. Your JS Fiddle works. I'm using your code: [ajax snip] 'success': function(obj) { //your loop goes here }); it gives me an Uncaught TypeError: Cannot read propertly 'length' of undefined -- it's not reading the data structs...
1
jsonObject.data[0][0];

there is no ID

Comments

1

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

1 Comment

Mike, thank you. I understand. But Karim's solution seems to have taken care of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.