2

我的 data.json 看起来像这样:

{
    "selection_form" : {
        "entities" : { 
            "name":"0002" ,
            "name":"0103" ,
            "name":"0104" ,
            "name":"0122" ,
....

这个脚本看起来像这样

  <script>     
            $.getJSON('data.json', function(data) {   
                $.each(data.selection_form.entities, function(i,item){
                    $("#enity").append('<p>'+data.selection_form.entities.name+'</p>');
                });
            });
        </script>

我想将所有名称都包含在 ap 标签中,例如

<p>0002</p><p>0103</p>....

但结果只是姓氏项目。我找不到解决办法。需要帮忙!

4

2 回答 2

1

一个对象不能有同名的属性。entities对象具有一个name值为 的属性0122。您应该更改属性名称。

{
    "selection_form": {
        "entities": {
            "name1": "0002",
            "name2": "0103",
            "name3": "0104",
            "name4": "0122",
        }
    }
}

$.getJSON('data.json', function(data) {   
      $.each(data.selection_form.entities, function(i,item){
           $("#enity").append('<p>'+item+'</p>');
      });
 });

http://jsfiddle.net/vB6qe/

于 2012-10-06T23:16:07.190 回答
0

您正在覆盖 JSON 属性名称。与其在那里有一个 JSON 对象,不如有一个数组更有意义:

{
    "selection_form" : {
        "entities" : [ "0002" , "0103" , "0104" , "0122" ]
    }
}


<script type="text/javascript">
    $.getJSON('data.json', function(data) {   
        $.each(data.selection_form.entities, function(i,name){
            $("#enity").append('<p>'+name+'</p>');
        });
    });
</script>

这是迭代该结构的演示

于 2012-10-06T23:29:44.790 回答