从 json 响应中获取值
我从服务器收到以下 JSON 响应:
{
"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
}]
}
}
我使用以下 javascript/jQuery 来获取值,但它没有显示任何内容:
obj = jQuery.parseJSON(responseText);
alert(obj.comments.iRecords[adate]);
注意:alert(obj.msg);
工作正常。
如何在 Javascript 中获取 adate 的值。
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未定义名为
adate
的变量,并且iRecords
是一个数组。如果您使用方括号表示法,则必须传入包含属性名称的字符串,而不是变量与属性同名。
You haven't defined a variable called
adate
andiRecords
is an arrayIf 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.
iRecords
保存一个对象数组,因此您需要访问数组的第一个索引才能获取第一个对象:或
iRecords
holds an array of objects, so you need to access the first index of the array to get to the first object:or
obj 有一个注释对象,该对象有一个 iRecods 成员,它是一个包含 1 个元素的数组;
obj has a comments object which has an iRecods member which is an array with 1 element so;