开发者

JSON data retrieval

I have an Array nested withing an Object that's inside an Object which is also in an Object.

EDIT: My original data structure was malformed, so here is a screen cap of console.log(data):

JSON data retrieval

This is being returned from an AJAX request and I'm having issue with getting to the array. The trick is that it will not always return 3 arrays per parent object, the amount of arrays is dependent on "length", or rather, "length" is a reflection of how many arrays to expect in each ob开发者_开发百科ject that contains arrays. "OID" is the name of each array for both the "th" and "uh" objects.

There also happen to be several main objects, which are iterated with for(var i in data)

I've tried something like:

for(var i in data) {
    for(var x in data[i].clicks) {
        //And now I'm lost here
        //I've tried getting the name of the Array from "OID"
        //and going from there, that failed miserably.
    }
}

How can I get at the contents of each of those arrays, it would be best if this can be done without knowing the name, quantity or length of said arrays.

Thanks in advance.


Your question and data structure is not very clear and I'm not familiar with the syntax you've used to declare your arrays, it seems incorrect to me.

If you intended your data structure be

data={
    "Clicks": {
            "length": 3,   //Can be between 0-3
            "OID": {
                1: "1",
                2: "2",
                3: "3"
            },
            "th": {
            1: [
                 null,
                null,
                null,
                null
            ],
            2: [
                null,
                null,
                null,
                null
            ],
            3: [
                null,
                null,
                null,
                null
            ]
            },
            "uh": {
           1: [
                 null,
                null,
                null,
                null
            ],
            2: [
                null,
                null,
                null,
                null
            ],
            3: [
                null,
                null,
                null,
                null
            ]
        }
    }
};

and what you want to do is to iterate over all the elements in th and uh where the key comes from the entries in OID, you should be doing something like

for(var i = 1; i <= data.Clicks.length; i++){
    data.Clicks.th[data.Clicks.OID[i]];
    data.Clicks.uh[data.Clicks.OID[i]];
}

However, if your keys are not going to be anything but numbers, it seems like you might be better served by returning an array of arrays for each of th and uh:

data={
    "Clicks": {
            "th": [
                [
                     null,
                    null,
                    null,
                    null
                ],
                [
                    null,
                    null,
                    null,
                    null
                ],
                [
                    null,
                    null,
                    null,
                    null
                ]
            ],
            "uh": [
               [
                     null,
                    null,
                    null,
                    null
                ],
                [
                    null,
                    null,
                    null,
                    null
                ],
                [
                    null,
                    null,
                    null,
                    null
                ]
            ]
        }
    };

and access it as

//assuming th and uh are of the same length always
for(var i = 1; i <= data.Clicks.th.length; i++){
    data.Clicks.th[i];
    data.Clicks.uh[i];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜