简体   繁体   中英

Access / process each (nested) objects, arrays or JSON

I have looked for a couple of days for an answer and the closest to what i need is in a question already answered "Access / process (nested) objects, arrays or JSON" but the problem i'm having is that this answer will only return one nested object even though there may be two or three with the same key.

I am currently working on a SharePoint Project where i need to retrieve the number of "SOPs" for a specific position, im making an jquery ajax call to the list where the data is being pulled from and the data is returned similar to this example, i need to be able to return each data(key):value but everything i've tried only returns the last key please help.

var root = {
leftChild: {
    leftChild: {
        leftChild: null,
        rightChild: null,
        data: 42
    },
        leftChild: {
    leftChild: {
        leftChild: null,
        rightChild: null,
        data: 142
    },           leftChild: {
    leftChild: {
        leftChild: null,
        rightChild: null,
        data: 242
    };

alert( root.rightChild.leftChild['data']);`

The JSON you provided is invalid, because an object has duplicate keys. Thus, your approach will not work. I would suggest correcting your data source, or using an array (without keys) instead of an object.

Alternatively, if you meant to use rightChild instead of leftChild, perhaps this JSON will make more sense:

var root = {
    leftChild: {
        leftChild: {
            leftChild: null,
            rightChild: null,
            data: 42
        },
        rightChild: {
            leftChild: {
                leftChild: null,
                rightChild: null,
                data: 142
            },
            rightChild: {
                leftChild: {
                    leftChild: null,
                    rightChild: null,
                    data: 242
                }
            }
        }
    }
};

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