简体   繁体   English

在innerHTML中显示JSON数组

[英]Display JSON arrays in innerHTML

so I received the following array from another student and my job is to display it in the HTML. 因此我从另一个学生那里收到了以下数组,我的工作是将其显示为HTML。

I've followed examples found on here as well as other sites. 我一直在这里和其他网站上找到示例。 It's my first attempt at doing this. 这是我第一次尝试这样做。

Anyway, I've called in the required values, but they're not displaying. 无论如何,我已经调用了必需的值,但是它们没有显示。 I'd appreciate any help I can get. 我将不胜感激。

This is the script block and I'll attach some of the HTML. 这是脚本块,我将附加一些HTML。 Currently the json is situated before the closing head tag. 目前,json位于关闭head标签之前。

 <script>

      var height = {"height" : "1.76m"};
      var weight = {"weight" : "65kg"};
      var bmi = {weight/(height*height)};
      var cholesterol = {"cholesterol" : "26mmol/d"};
      var glucose ={"glucose" : "100mg/dl"};
      var pressure = {"pressure" : "120/80"};
      var pulseRate = {"pulse rate" : "80bpm"};

      window.onload = function() {

        obj = JSON.parse(height, weight, bmi, cholesterol, glucose, pressure, pulseRate);

        document.getElementById("hgt").innerHTML = obj.height[1];
        document.getElementById("wgt").innerHTML = obj.weight[1];
        document.getElementById("bmi").innerHTML = obj.bmi[1];
        document.getElementById("chol").innerHTML = obj.cholesterol[1];
        document.getElementById("gluc").innerHTML = obj.glucose[1];
        document.getElementById("bp").innerHTML = obj.pressure[1];
        document.getElementById("rpr").innerHTML = obj.pulseRate[1];

      };

 </script>

      <div class="col-xs-2">
        <h3 class="heading">Today</h3>
        <img class="picture2" src="images/icon-height.png" />
      <div class="caption">
       <h2 id="hgt"></h2>
       <p>height</p>
      </div>

Since your variables height , weight ... do not have to be parsed, since they are valid JSON Objects, you could save yourself some time by assigning obj like this: 由于您的变量heightweight不必解析,因为它们是有效的JSON对象,因此可以通过分配obj来节省一些时间,如下所示:

var obj = {
   height: "1.76",
   weight: "65kg",
   ...,
   bmi: //your calculation here!
};

document.getElementById("yourid").innerHTML = obj.weight;
...

Since JSON objects use key : value pairs, you can simply access the values by using object.key or object['key'] . 由于JSON对象使用keyvalue对,因此您只需使用object.keyobject['key']访问值即可。

Since you're trying to access the values by using obj.weight[1] I'm guessing that you're trying to get the second value in { "weight" : "67kg" } . 由于您尝试使用obj.weight[1]访问这些值, obj.weight[1]我猜测您正在尝试获取{ "weight" : "67kg" }的第二个值。 But since this is a valid Object, weight is the key and "67kg" is the value. 但是,由于这是一个有效的对象,因此weight是关键,值是"67kg" So you are trying to get the value at index 1 on an Object ( obj.weight[1] ). 因此,您尝试获取对象( obj.weight[1] )的索引1处的值。 For this to work your object must have a key 1 or be an array. 为此,您的对象必须具有键1或为数组。 I hope you get what I'm trying to explain to you. 希望您能得到我想向您解释的内容。

To make it a lot more simpler, you could just use height.height instead of the not working obj.height[1] , because your var height is an object with a key height . 为了使其更简单,您可以只使用height.height而不是不起作用的obj.height[1] ,因为您的var height是具有key height的对象。 But for readability I would suggest the format from my snippet. 但是出于可读性考虑,我建议使用摘要中的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM