我有一个GET请求,发回一个像这样结构的JSON对象:
{"ID":5176,
"DateFrom":"8/29/2018",
"DateTo":"8/29/2018",
"Units":[{"Key":"Value","Key2": "Value2"}]
}
我无法访问Units [0]值。我尝试过以下方法:
testFunction(){
this.service.getJSON(params)
.subscribe((data) => {
this.model = data;
this.dateFrom = this.model.DateFrom;
this.dateTo = this.model.DateTo;
this.unit = this.model.Units.Key;
console.log(this.unit); //undefined
});
}
}
ngOnInit() {
this.testFunction();
}
我错过了什么?
你应该用
this.unit = this.model.Units[0].Key;
代替
this.unit = this.model.Units.Key;
因为Units.Key
未定义