简体   繁体   English

在React中解析JSON输入时出错

[英]Errors in parsing a JSON input in React

I'm writing a React App that gets JSON data from an API and shows some of it's contents. 我正在编写一个React App,它从API获取JSON数据并显示其中的一些内容。

This is the data it gets: 这是它得到的数据:

{"id":6,"name":"5 Storey I=0.7","Value":1344981250,"NSt":5,"jRatio":"[0.2,0.4,0.4]","jEDR":"[0.02,0.1,0.5,1]"}

And this is the App: 这是应用程序:

class App extends React.Component{
    constructor(props){
        super(props);
        this.state={
            data: [],
            isLoading: false,
            error: null,
        };
    }
    componentDidMount(){
        this.setState({isLoading: true});
        axios.get(API)
            .then(response => console.log(response.data.Value))
            .then(response => this.setState({data: response.data, isLoading: false}))
            .catch(response => this.setState({error: response.error, isLoading: false}));
    }
    render(){
        return (
            <div>
            <p>{this.state.error}</p>
            <p>{this.state.isLoading ? 'Loading...':'Loaded'}</p>
            <ul>{this.state.data.Value.toString()}</ul>
            </div>
        )
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

I get the Value "1344981250" in console but the page throws this error: 我在控制台中获得值“1344981250”,但该页面抛出此错误:

TypeError: this.state.data.Value is undefined TypeError:this.state.data.Value未定义

and in console: 并在控制台:

The development server has disconnected. 开发服务器已断开连接。 Refresh the page if necessary. 必要时刷新页面。

I also tried this: 我也试过这个:

this.state.data.map(obj => <li key={obj.toString()}>{obj.toString()}</li>)

and this time without any error, nothing shows up on the page. 这次没有任何错误,页面上没有任何内容。 (this one works for arrays of objects but shows nothing when it's just one object) (这个适用于对象数组,但只有一个对象时才显示任何内容)

So what did I miss? 那么我错过了什么?

TypeError: this.state.data.Value is undefined TypeError:this.state.data.Value未定义

Because data is an array and [].Value (any key) will be undefined. 因为数据是一个数组而且[].Value (任何键)都是未定义的。

Check this snippet: 检查此代码段:

 let data = []; console.log('data.Value = ', data.Value); // this will throw error: console.log('data.value.toString', data.Value.toString()) 


You defined the initial value of data as an array, and you are getting the object from api call. 您将数据的初始值定义为数组,并从api调用获取对象。 Solution is define the initial value as: 解决方案是将初始值定义为:

data: {}

And inside render method, write it like this: 在render方法中,像这样写:

<ul>{(this.state.data.Value || '').toString()}</ul>

You need to return the response from .then : 你需要从.then返回响应:

axios.get(API)
  .then(response => {console.log(response.data.Value); return response;}   //<=== here
  .then(response => this.setState({data: response.data, isLoading: false}))
  .catch(response => this.setState({error: response.error, isLoading: false}))

Edit: 编辑:

componentDidMount will get called after the initial rendering, so its better to define the initial value of isLoading: true , after that you can remove this: componentDidMount将在初始渲染后被调用,因此最好定义isLoading: true的初始值,之后你可以删除它:

this.setState({isLoading: true});

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

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