JavaScript json 值
我有一个 JSON var 响应
(见下文),但我无法使用 response.users[0].reputation
检索“reputation”的值。我在 Dashcode 中,它显示错误 Result of expression response.users[unknown] is not an object
。正确的语法是什么?
编辑:该变量是从 XMLHttpRequest
动态加载的。具有相同 json 的静态变量正在工作。
I have a JSON var response
(See below) but I can't retreive the value of "reputation" using response.users[0].reputation
. I am in Dashcode and it says in error Result of expression response.users[unknown] is not an object
. What is the correct syntax?
edit: The variable is dynamically loaded from a XMLHttpRequest
. A static var with same json is working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜想从
XMLHttpRequest
你收到字符串但不是 json 对象,所以你需要先解析它才能获取 json 对象,例如使用 JSON.Parse 或jQuery.parseJSON
和response.users[0].reputation
应该可以工作。I guess from
XMLHttpRequest
you recieve string but not json object, so you need parse it first in order to get json object, for example using JSON.Parse orjQuery.parseJSON
andresponse.users[0].reputation
should work.JSON 没问题。我刚刚检查了它和“response.users[0].reputation”。 (第二双眼睛等等。)
我会更关心“response.users[unknown]”中的“未知”。看起来您并不是在请求数组“users”的第 0 个索引。那里出了点问题。
The JSON is fine. I just checked it and "response.users[0].reputation". (Second pair of eyes and all that.)
I would be more concerned about the "unknown" in "response.users[unknown]". It doesn't look like you are requesting the 0th index of the array "users". Something's going wrong there.
就我个人而言,当我测试你的 Json 语法时,它是正确的:
http://www.jsonlint.com
我刚刚删除了评论“//需要获取其值”。
Personnally when i test the syntax of your Json, it is correct :
http://www.jsonlint.com
I have just deleted the comment "//need to get its value".
你的代码对我有用,就像你所做的那样。也许您没有正确分配变量(
var response = {"total": 1, etc...
)或在某处有拼写错误Your code works for me as you have done it. Maybe you are not assigning the variable correctly (
var response = {"total": 1, etc...
) or have a typo somewhere