javascript-用于(json中的键)-我想获取所有其他关键说明json仅包含对象 [英] javascript - using for (key in json) - I would like to get every other key note json consists only of objects

查看:50
本文介绍了javascript-用于(json中的键)-我想获取所有其他关键说明json仅包含对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用for (key in json),但只能使用其他所有键.使用数组会很简单,但是我收到的json只包含对象.

I would like to use for (key in json) but only use every other key. It would be simple using arrays, but the json I recieve consists only of objects.

我已经问了一个smililair问题,好心人指出了一个错字以及为什么它不起作用.问题(我仍然有)是内容重复(即:我希望id ="1"中的第一个对象和id ="2"中的2cnd对象,然后ID == 3中的第三个对象再次出现) "1"和id ="2"等中的第四个对象)

I have already asked a smililair question and th nicee people pointed out a typo and why it didn't work. The problem (I am still having) is that the content is duplicated (ie: I would like the 1st object in id="1" and the 2cnd object in id="2", then the 3rd object again in id="1" and the 4th object again in id="2" etc.)

问题是我在id ="1"中得到了第一个对象.且id ="1"和id ="2",则id ="1"中的2cnd对象再次出现.和id ="2".

The problem is that I get the 1st object in id="1" and id="2", then the 2cnd object again in id="1" and id="2".

例如,可以说我有这个json文件:

For example lets say I have this json file:

"123":{
   "name":"someName",
   "age":"12",
   "health":{
      "heart":"OK",
      "lungs":"Not so good"
    }
},
"223":{
   "name":"someName1",
   "age":"42",
   "health":{
      "heart":"Not so good",
      "lungs":"OK"
    }
}

我想将第一名患者的名字和肺脏设为div id ="1".并将2cnd人的名字和肺部标记为div id ="2".

I would like to get name and lungs of the 1st patient into div id="1" and the 2cnd persons name and lungs into div id="2".

What I would like:
id="1" ... SomeName, OK
id="1" ... SomeName1, Not so good

What I actually get is:
id="1" ... SomeName, OK, SomeName1, Not so good
id="2" ... SomeName, OK, SomeName1, Not so good

所以问题是如何从for (key in json)函数获取每个其他 key.

so the question is how do I get every other key from the for (key in json) function.

这是我的代码.抱歉,很长的文字,只是想让事情变得清楚.如果您还有其他疑问,请告诉我.谢谢.

Here is my code. Sorry for the long text, just wanted to make things clear. If you have any additional questions, let me know. Thanks.

    var data1 = document.getElementById("data1");
    var data2 = document.getElementById("data2");
    loadJSON(function(json) {
      var l = Object.keys(json).length;
      console.log("json start");
      for (var i = 0; i <= l; i++){
        console.log(i);
        if (i % 2 === 0){
          for (x in json) {
            data1.innerHTML+="<img src=" + json[x].picture + "/>";
            data1.innerHTML+=json[x].price_wrapper.price + json[x].price_thingy.suf + " modulo 0 + " + i;
            console.log("0" + i);
          }
        } else {
          for (x in json) {
            data2.innerHTML+="<img src=" + json[x].picture + "/>";
            data2.innerHTML+=json[x].price_wrapper.price + json[x].price_thingy.suf + " modulo 1 + " + i;
            console.log("1" + i);
          }
        } 
      }
    });

推荐答案

已解决.

功劳归@Ashwin.

The credit goes to @Ashwin.

他稍微修改了代码,现在可以了.

He reworked the code a little bit and now it works.

loadJSON(function(json) {
  console.log("json start");
  var i = 0;
  var l = Object.keys(json).length;
  for (x in json){
    console.log(i);
    if (i % 2 === 0){
      data1.innerHTML+="<img src=" + json[x].picture + "/>";
      data1.innerHTML+=json[x].price_thingy.price + json[x].price_thingy.suf;
      console.log("0 " + l);
    } else {
      data2.innerHTML+="<img src=" + json[x].picture + "/>";
      data2.innerHTML+=json[x].price_thingy.price + json[x].price_thingy.suf;
      console.log("1 " + l);
    }
    i++;
  }
});

这篇关于javascript-用于(json中的键)-我想获取所有其他关键说明json仅包含对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆