简体   繁体   English

使用JSON.parse(),如何在多维数组中取值?

[英]With use JSON.parse(), how to take value in multi dimensional array?

Before use JSON.parse, an JSON array is shown below: 在使用JSON.parse之前,JSON数组如下所示:

var temp = {
      "queries": [
    {
      "sample_size": 3,
      "results": [
        {
          "name": "temperature",
          "tags": {
            "Tag": [
              "temperature"
            ]
           },
           "values": [
            [
              1452221580000,
              27.5
            ],
            [
              1452221640000,
              27.1
            ],
            [
              1452221700000,
              27.3
            ]
           ]
        ]}
     ]}
    }

I need to get the value from the array, so I use JSON.parse(). 我需要从数组中获取值,所以我使用JSON.parse()。

var jsonparse_temp = JSON.parse(temp);
var dataNum = jsonparse_temp ['queries']['sample_size'];
var timestamp1 = jsonparse_temp ['queries']['results']['value'][0][0]
var value1 = jsonparse_temp ['queries']['results']['value'][0][1]

After that, is value1 equal 27.5 ? 之后, value1等于27.5吗? I'm not sure this way could sign the value to variable. 我不确定这种方式是否可以将值签名为变量。

It's not a JSON string it's a well-formed object no need to parse it. 它不是JSON字符串,而是格式正确的对象,无需解析它。 There is nested array so you need to get the object element from the array using the index. 有嵌套的数组,因此您需要使用索引从数组中获取对象元素。

 var temp = { "queries": [{ "sample_size": 3, "results": [{ "name": "temperature", "tags": { "Tag": [ "temperature" ] }, "values": [ [ 1452221580000, 27.5 ], [ 1452221640000, 27.1 ], [ 1452221700000, 27.3 ] ] }] }] }; // using bracket notation var dataNum = temp['queries'][0]['sample_size']; var timestamp1 = temp['queries'][0]['results'][0]['values'][0][0] var value1 = temp['queries'][0]['results'][0]['values'][0][1]; console.log(dataNum, timestamp1, value1); // using dot notation var dataNum1 = temp.queries[0].sample_size; var timestamp11 = temp.queries[0].results[0].values[0][0] var value11 = temp.queries[0].results[0].values[0][1]; console.log(dataNum1, timestamp11, value11); 

an JSON array is shown below: JSON数组如下所示:

That isn't JSON. 那不是JSON。 That is a JavaScript object. 那是一个JavaScript对象。 There is no JSON anywhere in your question. 您的问题中的任何地方都没有JSON。

 var jsonparse_temp = JSON.parse(temp); 

This will return undefined since temp is not a string of JSON. 由于temp不是JSON字符串,因此将返回undefined

 ['queries']['sample_size']; 

queries is an array, not an object. 查询是一个数组,而不是一个对象。 It doesn't have a sample_size property. 它没有sample_size属性。

The first value in that array is an object which does have that property. 该阵列中的第一个值是一个对象,它确实有该属性。

You need to access each level of the data structure in order. 您需要按顺序访问数据结构的每个级别。 You can't skip levels. 您不能跳过关卡。

var dataNum = temp.queries[0].sample_size;

change your code to this: 将您的代码更改为此:

 var dataNum = temp['queries']['sample_size']; var timestamp1 = temp['queries']['results']['value'][0][0] var value1 = temp['queries']['results']['value'][0][1] 

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

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