首页 > 解决方案 > 在 Angular 6 模板中解析 Json 数据

问题描述

我收到如下 JSON 响应,

{
  "Meta Data" : {
    "ticker" : "NEE",
    "datapoints" : 320,
    "credit cost" : 320,
    "Time Zone" : "America/New_York",
    "last refreshed" : "06/17/2019 at 09:27AM ET"
  },
  "Realtime Stock price" : [ {
    "ticker" : "NEE",
    "date" : "20190614",
    "minute" : "15:58:01",
    "volume" : 1028861,
    "price" : 207.02,
    "timezone" : "EDT",
    "timezone_name" : "America/New_York",
    "gmt_offset" : -14400
  }, {
    "ticker" : "NEE",
    "date" : "20190614",
    "minute" : "15:57:00",
    "volume" : 996057,
    "price" : 207.14,
    "timezone" : "EDT",
    "timezone_name" : "America/New_York",
    "gmt_offset" : -14400
  }]}

这是我的代码,

  allPrice: Array<object> = [];
getShareData(){
    this.dataservice.getSharePrice().subscribe(data=>{
      this.allPrice=data['Realtime Stock pric'];
      console.log("Show Price"+JSON.stringify(this.allPrice[1]));

    })
  }

但我没有在我的模板中获得价格价值。我也骑着地图

this.allPrice=data['Realtime Stock pric'].map(d=>d.price); 

但这也行不通。

我能做些什么 ?请建议

标签: htmlcssangularangularjs-directive

解决方案


它应该是

this.allPrice=data['Realtime Stock price']; 

不是

this.allPrice=data['Realtime Stock pric'];

推荐阅读