的getJSON显示[对象对象],而不是实际值对象、而不是、实际、getJSON

2023-09-10 15:01:42 作者:今晚去捕月

我有一个JSON,我需要得到这个JSON和放在HTML作为一个UL李名单。它得到的价值为目标,并显示在HTML中[对象的对象。如果我修改了JSON,然后它的作品。所以有可能是在那里我不能循环throught他JSON文件正确的东西错在我的剧本。能有人帮助,请:

我的JSON是:

  [
    {
        我们:USA
    },
    {
        FR:法国
    },
    {
        上课:西班牙
    },
    {
        SA:南非
    }
]
 

和JS IS

 <脚本>
  $ .getJSON('jsonfile',功能(数据){
    变种项= [];
    $每个(数据,功能(键,VAL){
      items.push('<李ID =+按键+'>+ VAL +'< /李>');
    });
    $('< UL />',{
      类:新格,
      的HTML:items.join('')
    })appendTo(身体)。
  });
< / SCRIPT>
 

修订JSON:

  [
{
    项目:
        {
            项目:
                [
                    {
                        ID:0001,
                        类型:环形,
                        名:蛋糕,
                        PPU:0.55,
                        击球:
                            {
                                连击:
                                    [
                                        {ID:1001,类型:常规},
                                        {ID:1002,类型:巧克力},
                                        {ID:1003,类型:蓝莓},
                                        {ID:1004,类型:恶魔的食物}
                                    ]
                            },
                        一流:
                            [
                                {ID:5001,类型:无},
                                {ID:5002,类型:釉},
                                {ID:5005,类型:糖},
                                {ID:5007,类型:糖粉},
                                {ID:5006,类型:巧克力洒},
                                {ID:5003,类型:巧克力},
                                {ID:5004,类型:枫}
                            ]
                    }


                ]
        }
}
]
 
jquery实现下拉菜单的二级联动利用json对象从DB取值显示联动

解决方案

数据你遍历是阵列,其中有物。所以,你的 0 1 等。和 VAL 将成为对象的在数组中的位置。

您的JSON结构实际上使得它有点痛输出,因为你的每个对象只有一个属性,但属性名的对象不同对象。您的可以的做到这一点,通过循环对象属性即使只有其中之一:

  VAR项目= [];
$每个(数据,功能(outerKey,outerVal){//< ==遍历数组
  $每个(outerVal,功能(键,VAL){//< ==循环通过每一个对象的属性
      items.push('<李ID =+按键+'>+ VAL +'< /李>');
  });
});
 

...但我改变JSON结构代替。例如,假设键是独一无二的,你原来的code将与这种结构:

  {
    我们:USA,
    FR:法国,
    上课:西班牙,
    SA:南非
}
 

I have a JSON and I need to get this JSON and put in the html as a ul li list. It gets the value as object and displays [object Object] in html. If I modify the json then it works. so there is probably something wrong in my script where I am not able to loop throught he json file properly. Can some one help please:

MY JSON IS:

[
    {
        "us":"USA"  
    },
    {
        "fr":"FRANCE"
    },
    {
        "es":"Spain"
    },
    {
        "sa":"South Africa"
    }
]

AND JS IS

<script>
  $.getJSON('jsonfile', function(data) {
    var items = [];
    $.each(data ,function(key,val) {
      items.push('<li id="'+ key +'">' + val +'</li>');
    });
    $('<ul />' , {
      'class':'new-div',
      html:items.join('')
    }).appendTo('body');
  });
</script>

UPDATED JSON:

[
{
    "items":
        {
            "item":
                [
                    {
                        "id": "0001",
                        "type": "donut",
                        "name": "Cake",
                        "ppu": 0.55,
                        "batters":
                            {
                                "batter":
                                    [
                                        { "id": "1001", "type": "Regular" },
                                        { "id": "1002", "type": "Chocolate" },
                                        { "id": "1003", "type": "Blueberry" },
                                        { "id": "1004", "type": "Devil's Food" }
                                    ]
                            },
                        "topping":
                            [
                                { "id": "5001", "type": "None" },
                                { "id": "5002", "type": "Glazed" },
                                { "id": "5005", "type": "Sugar" },
                                { "id": "5007", "type": "Powdered Sugar" },
                                { "id": "5006", "type": "Chocolate with Sprinkles" },
                                { "id": "5003", "type": "Chocolate" },
                                { "id": "5004", "type": "Maple" }
                            ]
                    }


                ]
        }
}
]

解决方案

The data you're looping over is the array, which has objects. So your key will be 0, 1, etc., and the val will be the object at that position in the array.

Your JSON structure actually makes it a bit of a pain to output, because each of your objects only has one property, but the property name varies from object to object. You can do it, by looping over the object properties even though there's only one of them:

var items = [];
$.each(data ,function(outerKey, outerVal) { // <== Loops through the array
  $.each(outerVal, function(key, val) {     // <== "Loops" through each object's properties
      items.push('<li id="'+ key +'">' + val +'</li>');
  });
});

...but I'd change the JSON structure instead. For instance, assuming the keys are unique, your original code would work with this structure:

{
    "us":"USA",
    "fr":"FRANCE",
    "es":"Spain",
    "sa":"South Africa"
}