我有以下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 )
})
}
答案 0 :(得分:3)
f['daypart']
是一个数组。因此,您需要按其索引引用它。
this.dforecast = f['daypart'][0]['narrative'];
此外,由于我们正在处理可观察对象而不是基于承诺的响应,因此无需将forcast()
声明为async
方法。
forcast() {
// rest of your cose
}
答案 1 :(得分:2)
您应该尝试:
this.dforecast = f["daypart"][0]["dayOrNight"]
因为它是一个数组。