使用getjson读取json数据

发布于 2024-11-15 21:50:09 字数 1323 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时间你老了 2024-11-22 21:50:09

试试这个:

 $.getJSON('asv/index.json', 
function(json) {
    $.each(json.bible.testament[0].book,  // $.each() looping on each books
    function(i, value) {
        console.log(value['@attributes'].name);  // here you get the name of books
    });

try this:

 $.getJSON('asv/index.json', 
function(json) {
    $.each(json.bible.testament[0].book,  // $.each() looping on each books
    function(i, value) {
        console.log(value['@attributes'].name);  // here you get the name of books
    });
一刻暧昧 2024-11-22 21:50:09

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

泛滥成性 2024-11-22 21:50:09

您的数据对象路径错误。我建议您将 json 数据粘贴到查看器中,以便更轻松地查看您需要获取的内容。例如,尝试 http://jsonviewer.stack.hu/

<script type="text/javascript">
$(document).ready(function(){
    $.getJSON("asv/index.json", function(json) {
        alert(json.bible.testament[0]['@attributes'].name);
        alert(json.bible.testament[0].book[0]['@attributes'].name);
    });
});
</script>

这对我有用。请注意,您没有任何 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.

<script type="text/javascript">
$(document).ready(function(){
    $.getJSON("asv/index.json", function(json) {
        alert(json.bible.testament[0]['@attributes'].name);
        alert(json.bible.testament[0].book[0]['@attributes'].name);
    });
});
</script>

That works for me. Notice how you don't have any testament[1] index, only testament[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.

人间不值得 2024-11-22 21:50:09

如果您有支持 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
...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文