2016-04-24 90 views
0

我正在学习ReactJs并希望显示天气数据。该API网址还给:显示来自API的json数据

{ 
    "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. 

改成了

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 

我改变了它,它看起来仍然logically..but错误

+0

可否请你更新你已经显示出更清晰的JSON数据? 即'data = {}'带有正确格式化的花括号 –

回答

0

而不是设置wetterdata.response的,只需将其设置为data

这种方式可以达到整个对象。例如: -

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

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

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

感谢Emil ..迄今为止...仍然没有成功.. – Werner

相关问题