简体   繁体   English

无法访问嵌套的 JSON Object 键:Typescript 中的值

[英]Cannot access nested JSON Object key : value in Typescript

When I assign a TypeScript object to a key (temp) of a nested JSON object (main):当我将 TypeScript object 分配给嵌套 JSON ZA8CFDE6331149EB2AC96F8 的键(温度)时

weatherToday : any;

getTheWeather(city : String)
  {
    console.log("HOME.TS: Getting the weather...");
    this.weather.getWeather("Dublin").subscribe( data => {
      this.weatherToday = data.main.temp;

    });

  }

And then output the weatherToday object in home.html:然后 output 今日天气 object 在家里。html:

<p>{{weatherToday}}</p>

The output is as expected: 284.73 output 符合预期:284.73

However when I try to assign the Typescript to the overall parent JSON object:但是,当我尝试将 Typescript 分配给整个父级 JSON object 时:

this.weatherToday = data;

And attempt to then output the same as above by object.object.value syntax in my home.html:然后尝试 output 与上面相同的 object.object.value 语法在我的家中。ZFC35FDC70D52FC69D2569883

 <p>{{weatherToday.main.temp}}</p>

I get a exception:我得到一个例外:

undefined is not an object undefined 不是 object

This is the JSON structure:这是 JSON 结构:

{
  "coord": {
    "lon": -121.94,
    "lat": 37.7
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 284.73,
    "feels_like": 282.68,
    "temp_min": 283.71,
    "temp_max": 286.48,
    "pressure": 1025,
    "humidity": 62
  },
  "visibility": 10000,
  "wind": {
    "speed": 1.2,
    "deg": 63
  },
  "clouds": {
    "all": 1
  },
  "dt": 1608145061,
  "sys": {
    "type": 1,
    "id": 4446,
    "country": "US",
    "sunrise": 1608131813,
    "sunset": 1608166239
  },
  "timezone": -28800,
  "id": 5344157,
  "name": "Dublin",
  "cod": 200
}

So I am not quite sure as to why this exception is thrown.所以我不太确定为什么会抛出这个异常。 I am using Ionic v3.我正在使用离子 v3。

this.weather.getWeather("Dublin").subscribe(data => {
      this.weatherToday = data.main.temp;

    });
 <p>{{weatherToday}}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM