Py学习  »  Jquery

如何从多维JSON数组中检索值而不知道使用JavaScript/jQuery的密钥?

ninetofiver • 4 年前 • 247 次点击  

我有一个包含多维数组的JSON文件。第一层是城市,第二层是温度数据。动态地从第二级提取值时遇到问题。

我尝试了以下方法:

console.log(Object.values(json[0])[3][1]) but it gives me errors.

不过,这很管用:

console.log(Object.values(json[0])[3])

以下是我的JSON代码示例:

[
  {
    "id": 1,
    "city": "Amsterdam",
    "country": "Netherlands",
    "monthlyAvg": [
      {
        "high": 7,
        "low": 3,
        "dryDays": 19,
        "snowDays": 4,
        "rainfall": 68
      },
      {
        "high": 6,
        "low": 3,
        "dryDays": 13,
        "snowDays": 2,
        "rainfall": 47
      },
      {
        "high": 10,
        "low": 6,
        "dryDays": 16,
        "snowDays": 1,
        "rainfall": 65
      },
      {
        "high": 11,
        "low": 7,
        "dryDays": 12,
        "snowDays": 0,
        "rainfall": 52
      },
      {
        "high": 16,
        "low": 11,
        "dryDays": 15,
        "snowDays": 0,
        "rainfall": 59
      },
      {
        "high": 17,
        "low": 11,
        "dryDays": 14,
        "snowDays": 0,
        "rainfall": 70
      },
      {
        "high": 20,
        "low": 12,
        "dryDays": 14,
        "snowDays": 0,
        "rainfall": 74
      },
      {
        "high": 20,
        "low": 12,
        "dryDays": 15,
        "snowDays": 0,
        "rainfall": 69
      },
      {
        "high": 17,
        "low": 10,
        "dryDays": 14,
        "snowDays": 0,
        "rainfall": 64
      },
      {
        "high": 14,
        "low": 9,
        "dryDays": 16,
        "snowDays": 0,
        "rainfall": 70
      },
      {
        "high": 9,
        "low": 6,
        "dryDays": 20,
        "snowDays": 1,
        "rainfall": 82
      },
      {
        "high": 7,
        "low": 1,
        "dryDays": 19,
        "snowDays": 1,
        "rainfall": 85
      }
    ]
  },
  {
    "id": 2,
    "city": "Athens",
    "country": "Greece",
    "monthlyAvg": [

我希望能够检索对应于high的值7。 我现在可以说:

json[0].monthlyAvg[0].high

如果不指定“.high”,如何获得结果

例如,这就是我想象的代码: 所以像这样:

“high”:7->我想通过键入类似json[0]的内容获得7。

“low”:3->我想通过键入类似json[0]的内容获得7。

“drydays”:19->我想通过键入类似json[0]的内容获得7.monthlyavg[0][2]

“Snowdays”:4->我想通过键入类似json[0]的内容获得7。

“降雨量”:68->我想通过键入类似json[0]的内容获得7。monthlyavg[0][4]

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46242
 
247 次点击  
文章 [ 2 ]  |  最新文章 4 年前