简体   繁体   中英

Get value from json response

I have following JSON response received from server:

   {
    "msg": "S",
    "comments": {
        "iRecords": [{
            "id": "9",
            "bid": "1",
            "uid": "5",
            "comment": "This is # 009",
            "adate": "Tuesday, 5th April, 2011 11:15:05",
            "status": "1",
            "userid": "5",
            "username": "pavlos",
            "oauthprovider": "l",
            "profile_link": null
        }]
    }
  }

Iam using following javascript/jQuery to get values but it is showing nothing:

obj = jQuery.parseJSON(responseText);
alert(obj.comments.iRecords[adate]);

Note: alert(obj.msg); is working fine.

How can I get value of adate in Javascript.

Thanks in advance

iRecords holds an array of objects, so you need to access the first index of the array to get to the first object:

obj = jQuery.parseJSON(responseText); 
alert(obj.comments.iRecords[0]["adate"]);

or

alert(obj.comments.iRecords[0].adate);

You haven't defined a variable called adate and iRecords is an array

If you use square bracket notation then you have to pass in a string containing the property name, not a variable with the same name as the property.

obj.comments.iRecords[0].adate;

obj有一个comment对象,该对象具有一个iRecods成员,该成员是一个包含1个元素的数组;因此,

x = obj.comments.iRecords[0].adate

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