使用getjson读取json数据
我有一个 json 文件:
{
"bible" : {
"@attributes" : {
"translation" : "ASV"
},
"testament" : [
{
"@attributes" : {
"name" : "Old"
},
"book" : [
{
"@attributes" : {
"name" : "Genesis"
}
},
{
"@attributes" : {
"name" : "Exodus"
}
},
{
"@attributes" : {
"name" : "Leviticus"
}
},
{
"@attributes" : {
"name" : "Numbers"
}
},
{
"@attributes" : {
"name" : "Deuteronomy"
}
},
{
"@attributes" : {
"name" : "Joshua"
}
},
{
"@attributes" : {
"name" : "Judges"
}
},
{
"@attributes" : {
"name" : "Ruth"
}
}
]
}
]
}
}
我正在使用代码来读取它:
$(document).ready(function(){
$.getJSON("asv/index.json", function(json) {
alert("JSON Data: " + json.bible.testament[1].name);
});
});
但这给了我未定义的信息。请告诉我如何读书名。另外@attributes有什么用? 谢谢
I have a json file:
{
"bible" : {
"@attributes" : {
"translation" : "ASV"
},
"testament" : [
{
"@attributes" : {
"name" : "Old"
},
"book" : [
{
"@attributes" : {
"name" : "Genesis"
}
},
{
"@attributes" : {
"name" : "Exodus"
}
},
{
"@attributes" : {
"name" : "Leviticus"
}
},
{
"@attributes" : {
"name" : "Numbers"
}
},
{
"@attributes" : {
"name" : "Deuteronomy"
}
},
{
"@attributes" : {
"name" : "Joshua"
}
},
{
"@attributes" : {
"name" : "Judges"
}
},
{
"@attributes" : {
"name" : "Ruth"
}
}
]
}
]
}
}
I am using code to read it:
$(document).ready(function(){
$.getJSON("asv/index.json", function(json) {
alert("JSON Data: " + json.bible.testament[1].name);
});
});
But this gives me undefined. Please let me know how to read book names. Also @attributes are what for?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
try this:
json.bible.testament[1].name
未定义。尝试 json.bible.testament[1]['@attributes'].name
json.bible.testament[1].name
is undefined.try
json.bible.testament[1]['@attributes'].name
您的数据对象路径错误。我建议您将 json 数据粘贴到查看器中,以便更轻松地查看您需要获取的内容。例如,尝试 http://jsonviewer.stack.hu/ 。
这对我有用。请注意,您没有任何
testament[1]
索引,只有testament[0]
。@attributes 部分似乎是生成 JSON 的脚本正在创建的内容,您无需使用 JSON 即可。如果我有权访问 JSON 创建脚本,我会删除它,但也许它在您看不到的某些系统中使用。
You have the wrong object path to your data. I recommend that you paste your json data into a viewer to make it easier to see what you need to get. Try http://jsonviewer.stack.hu/ for example.
That works for me. Notice how you don't have any
testament[1]
index, onlytestament[0]
.The @attributes part seems to be something the script that generates the JSON is creating, nothing you need to use JSON per say. I would remove it if I had access to the JSON-creating script, but perhaps it is used in some system that you do not see.
如果您有支持 console.log 的浏览器(例如 Firefox),您可以执行“console.log(json)”并查看结构。
您可以访问这样的名称:
json.bible.testament[0].book[0]['@attributes'].name
json.bible.testament[0].book[1]['@attributes'].name
...
If you have a browser that supports console.log (Firefox for example) you can do a 'console.log(json)' and look at the structure.
You can access names like that:
json.bible.testament[0].book[0]['@attributes'].name
json.bible.testament[0].book[1]['@attributes'].name
...