我有一个很复杂的JSON,我存储在一个String变量中。我想从中获得特定的元素。喜欢series_id,体育场。
我是angular2的新手,所以这让我很困惑。我是否需要将JSON分配给String以外的其他变量?如果是,我应该如何处理它?
"results": {
"Scorecard": {
"v": "0",
"mid": "196230",
"m": "3",
"series": {
"series_id": "12624",
"series_name": "Indian Premier League, 2017"
},
"ecf": "0",
"place": {
"vid": "90",
"stadium": "M.Chinnaswamy Stadium, Bengaluru",
"city": "Bengaluru",
"country": "India",
"Gimaget": "https://s.yimg.com/qx/cricket/fufp/images/venue_90_thumb-29-3-2011-55e03bbb85867160dc7f785dc204e8a4.jpg",
"date": "20170416143000",
"enddate": "20170416183000"
}
}
目前我使用以下代码:
export class CricketComponent {
getData: string;
ngOnInit() {
this._cricketService.getScore()
.subscribe(data => this.getData = JSON.stringify(data));
}
}
答案 0 :(得分:1)
<td>{{getData?.results?.series_id}}</td>
同时删除stringify
。只需使用
getData: string; ngOnInit() {
this._cricketService.getScore()
.subscribe(data => this.getData = data);
}
答案 1 :(得分:-1)
从服务返回的数据应该已经解析为json,您可以将数据用作javascript对象。这就是我的意思JSON是一个它不需要字符串化来读取其属性的对象。
getData: any;
ngOnInit() {
this._cricketService.getScore() .subscribe(data => {
this.getData = data;
console.log(this.getData.results.Scorecard.series.series_id);
}
}