简体   繁体   English

显示来自API的json数据

[英]Display json data from API

I am learning ReactJs and want to display weather data. 我正在学习ReactJs,想显示天气数据。 The API Url gives back: API网址返回:

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
  }
  ,"current_observation": {
        "image": {
        "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
        "title":"Weather Underground",
        "link":"http://www.wunderground.com"
        }, etc. etc.

Changed it to 更改为

componentDidMount() {
        $.ajax({
            url: "http://api.wunderground.com/api/*****/....",
            success: function(data){
                this.setState({
                    wetter: data
                });
            }.bind(this)
        });
    }

    render() {
        return <div>{this.state.wetter.response.termsofService}</div>;
    }

But only get console: Uncaught TypeError: Cannot read property 'termsofService' of undefined

I changed it and it looks logically..but still errors 我更改了它,看起来很合逻辑..但是还是错误

Instead of setting wetter to data.response , just set it to data . 相反设置的wetterdata.response ,只需将其设置为data

This way you can reach the entire object. 这样,您可以到达整个对象。 Eg: 例如:

return <div>{this.state.wetter.response.termsofService}</div>;
    // http://www.wunderground.com/weather/api/d/terms.html

or 要么

return <div>{this.state.wetter.response.features.conditions}</div>;
    // 1

or 要么

return <div>{this.state.wetter.current_observation.image.url}</div>;
    // http://icons.wxug.com/graphics/wu2/logo_130x80.png

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

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