首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON动态Javascript解析“未定义”

JSON动态Javascript解析“未定义”
EN

Stack Overflow用户
提问于 2015-02-13 11:59:36
回答 1查看 114关注 0票数 0

一个简单的问题:

这意味着变量r类似于:

代码语言:javascript
复制
{
"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方法:

代码语言:javascript
复制
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"?!?!是对象错误吗?

EN

回答 1

Stack Overflow用户

发布于 2015-02-13 12:01:58

因为它实际上是一个数组- definitions是一个数组。为了从你的问题中获得你期望的输出-

代码语言:javascript
复制
console.log(parsedJSONquery.definitions[0].text); 

您可以使用常用的数组函数来处理它-例如,在所有这些函数上调用console.log -

代码语言:javascript
复制
obj.definitions.forEach(function(el) { 
  console.log(el.text); 
}); 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28492262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档