一个简单的问题:
这意味着变量r类似于:
{
"definitions":[
{
"text":"An internet search, such as that which is performed on the Google search engine.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
},
{
"text":"A match obtained by a query in the Google search engine.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
},
{
"text":"To search for (something) on the Internet using the Google search engine.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
},
{
"text":"To search for (something) on the Internet using any comprehensive search engine.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
},
{
"text":"To be locatable in a search of the Internet.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
},
{
"text":"To deliver googlies.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
},
{
"text":"To move as a ball in a googly.",
"attribution":"from Wiktionary, Creative Commons Attribution/Share-Alike License"
}
]
}作为JSON字符串,我使用常规的JSON.parse方法:
var parsedJSONquery =JSON.parse(r);为什么它在控制台中显示,当我简单地调用像console.log(parsedJSONquery.definitions.text),这样的东西时,它返回“未定义的”,为什么它不登录到控制台“互联网搜索,如在谷歌搜索引擎上执行的搜索”。比如说?
建议之后:谢谢你的帮助,但我真正想做的是从php文件中调用一些JSON (作为变量r),我认为这与我在这里所要求的没有什么不同,但是当我从php文件中动态调用JSON时,它会给我一个错误(我看不出本地变量和$.ajax({ url:"url",function(r){ var parsedJSONquery=JSON.parse(r); console.log(parsedJSONquery.definitions[0].text); }})中的response变量之间有什么区别?)当我尝试对动态调用的变量r使用您的答案时,我得到: SyntaxError: JSON解析错误:意外标识符"object"?!?!是对象错误吗?
发布于 2015-02-13 12:01:58
因为它实际上是一个数组- definitions是一个数组。为了从你的问题中获得你期望的输出-
console.log(parsedJSONquery.definitions[0].text); 您可以使用常用的数组函数来处理它-例如,在所有这些函数上调用console.log -
obj.definitions.forEach(function(el) {
console.log(el.text);
}); https://stackoverflow.com/questions/28492262
复制相似问题