简体   繁体   English

来自 JSON 响应的 Angular 5 显示结果

[英]Angular 5 display result from JSON response

I am trying to access the "list" parameter in the following data set received from [Open weather map][1].我正在尝试访问从 [Open weather map][1] 收到的以下数据集中的“list”参数。 I basically need to access the list layer in the below set where I can get the temp parameter.我基本上需要访问下面集合中的列表层,我可以在其中获取临时参数。

{  
"cod":"200",
"message":0.0046,
"cnt":37,
"list":[  
  {  
     "dt":1518080400,
     "main":{  
        "temp":297.81,
        "temp_min":295.457,
        "temp_max":297.81,
        "pressure":1011.64,
        "sea_level":1018.79,
        "grnd_level":1011.64,
        "humidity":71,
        "temp_kf":2.35
     },
     "weather":[  
        {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
        }
     ],
     "clouds":{  
        "all":0
     },
     "wind":{  
        "speed":3.76,
        "deg":322.502
     },
     "sys":{  
        "pod":"d"
     },
     "dt_txt":"2018-02-08 09:00:00"
  },
  {  
     "dt":1518091200,
     "main":{  
        "temp":298.03,
        "temp_min":296.468,
        "temp_max":298.03,
        "pressure":1010.47,
        "sea_level":1017.64,
        "grnd_level":1010.47,
        "humidity":65,
        "temp_kf":1.57
     },
     "weather":[  
        {  
           "id":802,
           "main":"Clouds",
           "description":"scattered clouds",
           "icon":"03d"
        }
     ],
     "clouds":{  
        "all":48
     },
     "wind":{  
        "speed":4.77,
        "deg":315
     },
     "sys":{  
        "pod":"d"
     },
     "dt_txt":"2018-02-08 12:00:00"
  },
  {  
     "dt":1518102000,
     "main":{  
        "temp":294.89,
        "temp_min":294.104,
        "temp_max":294.89,
        "pressure":1011.17,
        "sea_level":1018.11,
        "grnd_level":1011.17,
        "humidity":77,
        "temp_kf":0.78
     },
     "weather":[  
        {  
           "id":802,
           "main":"Clouds",
           "description":"scattered clouds",
           "icon":"03d"
        }
     ],
     "clouds":{  
        "all":44
     },
     "wind":{  
        "speed":4.91,
        "deg":287.002
     },
     "sys":{  
        "pod":"d"
     },
     "dt_txt":"2018-02-08 15:00:00"
  }
]}

I am not sure as to how to go about it.我不知道如何去做。 I keep on getting this error "ERROR Error: Cannot find a differ supporting object"我不断收到此错误“错误错误:找不到不同的支持对象”

I tried looping through it like below我试着像下面这样循环

this.http.get('http://api.openweathermap.org/data/2.5/forecast?id=3362024&APPID=bbcf57969e78d1300a815765b7d587f0').subscribe(data => {
    this.items = JSON.stringify(data);
    console.log(this.items);
    for(var i = 0; i < this.items.length; i++){
      this.min = this.items[i].dt;
      console.log(this.min);
    }
  });

Do console.log(data);做 console.log(data); and check what kind of data you are getting from API.并检查您从 API 获得的数据类型。

If you are getting JSON data from API, then do not do JSON.stringify(data);如果您从 API 获取 JSON 数据,则不要执行 JSON.stringify(data);

If you are getting JSON contained in string then do JSON.parse();如果你得到包含在字符串中的 JSON,那么执行JSON.parse();

After this you will get JSON in a variable and you can iterate it as follows在此之后,您将在变量中获得 JSON,您可以按如下方式对其进行迭代

Also, do not post your api key in question , others can hit API using your api key另外,不要发布有问题的 api 密钥,其他人可以使用您的 api 密钥访问 API

this.http.get('http://api.openweathermap.org/data/2.5/forecast?id=yourId&APPID=yourapikey')
             .subscribe(data => {

             var res = JSON.parse(data); //if you are getting JSON in a string, else do res = data;

             for(var i = 0; i < res.list.length; i++){
                console.log(res.list[i].main.temp);
             }
         });

Try this.试试这个。 Make sure you import following import on top of the component确保在组件顶部导入以下导入

import 'rxjs/Rx';

or

import 'rxjs/add/operator/map'

     getData(){
        this.http.get('https://api.openweathermap.org/data/2.5/forecast?id=3362024&APPID=bbcf57969e78d1300a815765b7d587f0')
.map(res=>res.json()).subscribe(data => {
        this.items = data;
        console.log(this.items);
        for(var i = 0; i < this.items.list.length; i++){
          this.min = this.items.list[i].main;
          console.log(this.min);
        }
      });
      }

WORKING DEMO 工作演示

Considering you are correctly getting json response:=> One way is : if you know response in advance and its basic structure is always same then: you can create a model object similar to the json response and assign the json response to that object and access any values.考虑到您正确获得 json 响应:=> 一种方法是:如果您事先知道响应并且其基本结构始终相同,则:您可以创建一个类似于 json 响应的模型对象并将 json 响应分配给该对象并访问任何值。

eg例如

    export class TopLayer{
      fieldName1: dataType;
      fieldName2: Array<SecondLayer>;
    }
    export class SecondLayer{
      fieldName1: datatype;
      fieldName2: ThirdLayer;
    }
export class ThirdLayer{
   fieldName: datatype
}

another is: assign your json response to a var variable then access what you need: eg另一个是:将您的 json 响应分配给 var 变量,然后访问您需要的内容:例如

    var x = response;
         var list = x.list;

We can also do:我们还可以这样做:

this.http.get("some-api-url")
    .subscribe((response)=>{
      for (let key in response) {
        if (response.hasOwnProperty(key)) {
          let element = response[key];
          let singleData = {id: element.id, value: element.value};
          this.dataArray.push(singleData);
        }
      }

    },
    (error)=>{
      console.log(error)
    });

When the response is like [{}, {}, ...]当响应类似于[{}, {}, ...]

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

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