简体   繁体   中英

How to access an array in a JSON object?

I have the following JSON object:

[
    {
        "comments": [
            {
                "created_at": "2011-02-09T14:42:42-08:00",
                "thumb": "xxxxxxx",
                "level": 1,
                "id": 214,
                "user_id": 41,
                "parent_id": 213,
                "content": "<p>xxxxxx</p>",
                "full_name": "xx K"
            },
            {
                "created_at": "2011-02-09T14:41:23-08:00",
                "thumb": "xxxxxxxxxxxxx",
                "level": 0,
                "id": 213,
                "user_id": 19,
                "parent_id": null,
                "content": "<p>this is another test</p>",
                "full_name": "asd asd asd asd asd"
            }
        ],
        "eee1": "asdadsdas",
        "eee2": "bbbbb"
    }
]

This is coming from a $.ajax request, in success I have....

success: function (dataJS) {
    console.log(dataJS);
    console.log(dataJS[eee1]);
    console.log(dataJS.comments);
}

Problem is I can't get access to the items in the JSON object, even though dataJS does show correctly in the console. Ideas?

That's because your base object is an array as well.

console.log(dataJS[0].comments[0]);

I suspect that would work

the JSON you have coming back is actually an array itself, so...

dataJS[0].comments[0].created_at

will be 2011-02-09T14:42:42-08:00 , etc...

Both dataJS and comments are arrays, and need indexes to access the appropriate elements.

返回的对象本身就是一个数组,因此要获得第一个注释(作为示例),您可以通过以下方式访问它:

dataJS[0].comments[0]

console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);

Do something like this:-

var dataJS = [{"comments":[{"created_at":"2011-02-09T14:42:42-08:00","thumb":"xxxxxxx","level":1,"id":214,"user_id":41,"parent_id":213,"content":"<p>xxxxxx</p>","full_name":"xx K"},{"created_at":"2011-02-09T14:41:23-08:00","thumb":"xxxxxxxxxxxxx","level":0,"id":213,"user_id":19,"parent_id":null,"content":"<p>this is another test</p>","full_name":"asd asd asd asd asd"}],"eee1":"asdadsdas","eee2":"bbbbb"}];

var created_at = dataJS[0].comments[0].created_at;

Yes, as others have stated, the JSON is actually an Array (of a single Object). So you will need to reference an index.

Interestingly enough (to me), your result string does validate successfully as JSON. I assumed until now, that to be valid JSON, it had to be an Object (ie, {}).

You have to mention the dataType in the ajax request as 'JSON'. Make user you did that like below.

 $.ajax({
            url: $('#frmAddCourse').attr('action'),
            type: 'POST',
            data: $('#frmAddCourse').serialize(),
            dataType: 'JSON',
            success: function (data){
                Materialize.toast(data['state'],2000);
            },
            error:function(){
                Materialize.toast(errorMessage,2000);
            }
        });

JSON must be interpreted with eval function (after the obvious sanitization, see security considerations of eval). Are you sure your framework does that for you?

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