简体   繁体   English

如何在React上访问嵌套的JSON数据?

[英]How do you access nested JSON data on React?

I am having trouble accessing the JSON data obtained from the Accuweather API on React in the Render Method. 我在渲染方法中访问React上从Accuweather API获取的JSON数据时遇到问题。

I am doing this on React and get a undefined error even though I see the value in the JSON view when I open the link and format it. 我在React上执行此操作并获取未定义的错误,即使我在打开链接并格式化时在JSON视图中看到了该值。 I have tried using the dot method but no luck. 我尝试过使用点法,但没有运气。

import React, { Component } from 'react';

class App extends Component {

constructor(props){
super(props);
this.state = {
    items:[],
    isLoaded: false, 
}
}

componentDidMount() {


fetch('http://dataservice.accuweather.com/forecasts/v1/daily/1day/2094578.json?details=true&apikey=JjaFgoA67A3eXoMR7SiRyprGyPiv4Eln')

.then(res => res.json())  
.then(json => {
    this.setState({
        isLoaded:true, 
        items:json, 
      })
})
}
render() {
var {isLoaded, items} = this.state; 
if (!isLoaded) {
    return <div> Loading...</div>; 
}
else {
  return (
    <div className="App">

     <h1>IES Storm Water Forecast</h1>
     <h1>{new Date().toString()}</h1>
     <h2> Weather Forcast: {items["DailyForecasts"]["Day"]["ShortPhrase"]}</h2>

     <h3>Description: {items.Headline.Text}</h3>
     <h6>Source: Accuweather</h6>
    </div>

);
}

}

}

export default App;

JSON object in there: 那里的JSON对象:

http://dataservice.accuweather.com/forecasts/v1/daily/1day/2094578.json?details=true&apikey=JjaFgoA67A3eXoMR7SiRyprGyPiv4Eln http://dataservice.accuweather.com/forecasts/v1/daily/1day/2094578.json?details=true&apikey=JjaFgoA67A3eXoMR7SiRyprGyPiv4Eln

I get the following errors: 我收到以下错误:

Unhandled Rejection (TypeError): Cannot read property 'ShortPhrase' of undefined TypeError: Cannot read property 'ShortPhrase' of undefined 未处理的拒绝(TypeError):无法读取未定义的TypeError属性'ShortPhrase':无法读取未定义的属性'ShortPhrase'

But I should be getting a short sentence on weather. 但我应该对天气做一个简短的判决。

The returned data has a structure like this: 返回的数据有如下结构:

obj = {
  DailyForecasts: [
    {
      Day: {
        Icon: 6,
        IconPhrase: "Mostly cloudy",
        ShortPhrase: "Mostly cloudy",
        LongPhrase: "Mostly cloudy"
      }
    }
  ]
};

You can see DailyForecasts returns an array . 您可以看到DailyForecasts返回一个数组 You will have to access this array before you access the object inside of it: 在访问其中的对象之前,您必须访问此数组:

items["DailyForecasts"][0]["Day"]["ShortPhrase"]

or with shorter, dot notation: 或者用较短的点符号表示:

items.DailyForecasts[0].Day.ShortPhrase

Add a template condition like the following: ... 添加如下模板条件:...

else {
  return (
    <div className="App">

     <h1>IES Storm Water Forecast</h1>
     <h1>{new Date().toString()}</h1>
     <h2> Weather Forcast: {items["DailyForecasts"]["Day"] && items["DailyForecasts"]["Day"]["ShortPhrase"]}</h2>
                            ^^^

     <h3>Description: {items.Headline.Text}</h3>
     <h6>Source: Accuweather</h6>
    </div>

);
}
...

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

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