访问第二级json对象

时间:2016-12-09 01:37:30

标签: javascript json

我有一个如下所示的数据对象:

[
      {
        "key": "Both", 
        "values": [
          {
            "key": 1420070400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1422748800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1425168000000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1427846400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1430438400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1433116800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1435708800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1438387200000, 
            "values": 27927.734942179697
          }, 
          {
            "key": 1441065600000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1443657600000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1446336000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1448928000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1451606400000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1454284800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1456790400000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1459468800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1462060800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1464739200000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1467331200000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1470009600000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1472688000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1475280000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1477958400000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1480550400000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1483228800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1485907200000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1488326400000, 
            "values": 36929.200142179696
          }
        ]
      }
    ]

我想要做的就是让它看起来像这样:

var data_costByScn2 = data3.values

console.log("data3val: ", data_costByScn2)

我试过这个:

{{1}}

但它返回undefined ...如何让它返回json对象中的下一个级别?

3 个答案:

答案 0 :(得分:3)

您正在寻找第一个数组元素的values

var data_costByScn2 = data3[0].values

console.log("data3val: ", data_costByScn2)



var data3 = [
  {
    "key": "K1204", 
    "values": [
      {
        "key": "Both", 
        "values": [
          {
            "key": 1420070400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1422748800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1425168000000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1427846400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1430438400000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1433116800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1435708800000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1438387200000, 
            "values": 27927.734942179697
          }, 
          {
            "key": 1441065600000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1443657600000, 
            "values": 27927.7349421797
          }, 
          {
            "key": 1446336000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1448928000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1451606400000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1454284800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1456790400000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1459468800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1462060800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1464739200000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1467331200000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1470009600000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1472688000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1475280000000, 
            "values": 36929.2001421797
          }, 
          {
            "key": 1477958400000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1480550400000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1483228800000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1485907200000, 
            "values": 36929.200142179696
          }, 
          {
            "key": 1488326400000, 
            "values": 36929.200142179696
          }
        ]
      }
    ]
  }
]

var data_costByScn2 = data3[0].values

console.log("data3val: ", data_costByScn2)




答案 1 :(得分:2)

因为第一个对象实际上是一个包含1个项目的Array [...],data3 [0] .values实际上会为您提供您正在寻找的值。

答案 2 :(得分:1)

您需要选择0索引元素的第一个元素:

尝试Object[0].values

示例代码段:



var obj = [{
  "key": "K1204",
  "values": [{
    "key": "Both",
    "values": [{
      "key": 1420070400000,
      "values": 27927.7349421797
    }, {
      "key": 1422748800000,
      "values": 27927.7349421797
    }, {
      "key": 1425168000000,
      "values": 27927.7349421797
    }, {
      "key": 1427846400000,
      "values": 27927.7349421797
    }, {
      "key": 1430438400000,
      "values": 27927.7349421797
    }, {
      "key": 1433116800000,
      "values": 27927.7349421797
    }, {
      "key": 1435708800000,
      "values": 27927.7349421797
    }, {
      "key": 1438387200000,
      "values": 27927.734942179697
    }, {
      "key": 1441065600000,
      "values": 27927.7349421797
    }, {
      "key": 1443657600000,
      "values": 27927.7349421797
    }, {
      "key": 1446336000000,
      "values": 36929.2001421797
    }, {
      "key": 1448928000000,
      "values": 36929.2001421797
    }, {
      "key": 1451606400000,
      "values": 36929.2001421797
    }, {
      "key": 1454284800000,
      "values": 36929.200142179696
    }, {
      "key": 1456790400000,
      "values": 36929.2001421797
    }, {
      "key": 1459468800000,
      "values": 36929.200142179696
    }, {
      "key": 1462060800000,
      "values": 36929.200142179696
    }, {
      "key": 1464739200000,
      "values": 36929.200142179696
    }, {
      "key": 1467331200000,
      "values": 36929.2001421797
    }, {
      "key": 1470009600000,
      "values": 36929.2001421797
    }, {
      "key": 1472688000000,
      "values": 36929.2001421797
    }, {
      "key": 1475280000000,
      "values": 36929.2001421797
    }, {
      "key": 1477958400000,
      "values": 36929.200142179696
    }, {
      "key": 1480550400000,
      "values": 36929.200142179696
    }, {
      "key": 1483228800000,
      "values": 36929.200142179696
    }, {
      "key": 1485907200000,
      "values": 36929.200142179696
    }, {
      "key": 1488326400000,
      "values": 36929.200142179696
    }]
  }]
}]

console.log(obj[0].values)