首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示来自API的json数据

显示来自API的json数据
EN

Stack Overflow用户
提问于 2016-04-24 20:00:17
回答 1查看 267关注 0票数 0

我正在学习ReactJs,想要显示天气数据。API Url回馈:

代码语言:javascript
复制
{
  "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.

已更改为

代码语言:javascript
复制
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错误

EN

回答 1

Stack Overflow用户

发布于 2016-04-25 02:54:04

不要将wetter设置为data.response,只需将其设置为data即可。

这样,您就可以到达整个对象。例如:

代码语言:javascript
复制
return <div>{this.state.wetter.response.termsofService}</div>;
    // http://www.wunderground.com/weather/api/d/terms.html

代码语言:javascript
复制
return <div>{this.state.wetter.response.features.conditions}</div>;
    // 1

代码语言:javascript
复制
return <div>{this.state.wetter.current_observation.image.url}</div>;
    // http://icons.wxug.com/graphics/wu2/logo_130x80.png
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36822819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档