Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从服务器接收数据作为json数据,但无法解析react中的数据_Json_Reactjs - Fatal编程技术网

从服务器接收数据作为json数据,但无法解析react中的数据

从服务器接收数据作为json数据,但无法解析react中的数据,json,reactjs,Json,Reactjs,我正在从服务器接收数据,但无法解析它。当我解析时,我发现一个错误 “位置0处JSON中的意外标记u 在JSON.parse()处 从“React”导入React; 从“axios”导入axios; 类Premontessori扩展了React.Component{ 建造师(道具){ 超级(道具); 这个州={ 职位:[] }; } componentDidMount(){ axios.get()http://localhost:8080/list') .then(data=>this.setSt

我正在从服务器接收数据,但无法解析它。当我解析时,我发现一个错误 “位置0处JSON中的意外标记u 在JSON.parse()处

从“React”导入React;
从“axios”导入axios;
类Premontessori扩展了React.Component{
建造师(道具){
超级(道具);
这个州={
职位:[]
};
}
componentDidMount(){
axios.get()http://localhost:8080/list')
.then(data=>this.setState({post:data})
);
}
render(){
返回(
{JSON.parse(this.state.post.data)}
);
}
}
导出默认Premontessori;
这是:

axios.get('http://localhost:8080/list')
.then(data =>  this.setState({post:data} )
…将您的状态设置为
{post:…}
,其中
..
数据的值,该值可能是字符串或解析的对象树,具体取决于
axios.get
在接收到JSON时是否自动解析JSON

如果它是字符串,则需要使用
JSON.parse
对其进行解析。然后直接使用它:

.then(data =>  this.setState(JSON.parse(data) )
…或者如果它真的是
post
的值,那么:

.then(data =>  this.setState({post: JSON.parse(data)} )
如果它已经被解析,那么我猜它不应该是
post
的值,因此:

.then(data =>  this.setState(data))

您正在将属性post设置为从服务器接收到的有效负载,因此您应该使用{JSON.parse(this.state.post)}而不是{JSON.parse(this.state.post.data)}。如果我这样做,我会收到一个错误,因为JSON.parse()处的JSON输入意外结束。您可以控制台.log(数据)并提供从服务器返回的内容吗?
.then(data =>  this.setState(data))