【问题标题】:Angular get json array inside another arrayAngular在另一个数组中获取json数组
【发布时间】:2019-08-09 21:08:38
【问题描述】:

我有以下 JSON 结构:

{
  "daypart": [
    {
      "dayOrNight": [
        null,
        "N",
        "D",
        "N",
        "D",
        "N",
        "D",
        "N",
        "D",
        "N",
        "D",
        "N"
      ]
    }
  ]
}

我想要做的是到达 narrative 内的键数组。但是每当我尝试获取这些密钥时,我都会得到预测 undefined 或预测 [object Object]

代码

 async forcast(){
    this.http.get("xxxxxxxxxxxxxxx")

    .subscribe(data => {

      let f = data
      this.dforecast = f["daypart"]["narrative"]


      console.log("forecast "+ this.dforecast )


    })
  }

【问题讨论】:

  • “叙事”从何而来?不是“白天或黑夜”吗?
  • 抱歉更新了

标签: arrays json angular


【解决方案1】:

f['daypart'] 是一个数组。因此,您需要通过它的索引来引用它。

this.dforecast = f['daypart'][0]['narrative'];

此外,无需将 forcast() 声明为 async 方法,因为我们处理的是可观察对象,而不是基于 Promise 的响应。

forcast() {
 // rest of your cose 
}

【讨论】:

    【解决方案2】:

    您应该尝试:

    this.dforecast = f["daypart"][0]["dayOrNight"]
    

    因为它是一个数组。

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多