我有一个很大的复杂 JSON,我已将其存储在字符串变量中。我只想从中获取特定元素。比如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));
}
}
最佳答案
<td>{{getData?.results?.series_id}}</td>
同时删除stringify
。只需使用即可
getData: string; ngOnInit() {
this._cricketService.getScore()
.subscribe(data => this.getData = data);
}
关于json - 如何从 JSON 选择元素 - Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43438720/