简体   繁体   中英

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):

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM