2016-04-24 20 views
0

私はReactJを学習しており、天気データを表示したいと考えています。 APIのURLは、バック与える: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. 

が、私はそれを変更し、それだけにそれを設定し、まだエラー

代わり data.responseから wetterを設定する
+0

あなたがより読みやすいことが示されてきたJSONデータを更新してくださいもらえますか? 適切に書式が設定された中カッコで 'data = {}' –

答えて

0

logically..butに見える

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 

にそれを変更します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

ありがとうエミール..これまで通りました。 – Werner