如何访问json数据中的单个数据
我从服务器接收json数据,如下所示如何访问json数据中的单个数据,json,reactjs,Json,Reactjs,我从服务器接收json数据,如下所示 [{ "_id": "588b3731d4428e2ff05ddefa", "Id": 50, "Name": "Vishruth", "Age": 11 }, { "_id": "588b45df255e323de55ac333", "Id": 51, "Name": "Vishruth", "Age": 11 }] 如何从中访问单个数据 import React from 'react';
[{
"_id": "588b3731d4428e2ff05ddefa",
"Id": 50,
"Name": "Vishruth",
"Age": 11
}, {
"_id": "588b45df255e323de55ac333",
"Id": 51,
"Name": "Vishruth",
"Age": 11
}]
如何从中访问单个数据
import React from 'react';
import axios from 'axios';
//require('./style.scss');
class Premontessori extends React.Component{
constructor(props){
super(props);
this.state={
post:[]
};
}
componentDidMount(){
let self = this;
axios.get('http://localhost:8080/list')
.then(function(data) {
console.log(data);
self.setState({post:data});
});
}
render(){
return(
<div>
{JSON.stringify(this.state.post.data)}
</div>
);
}
}
export default Premontessori;
从“React”导入React;
从“axios”导入axios;
//要求('./style.scss');
类Premontessori扩展了React.Component{
建造师(道具){
超级(道具);
这个州={
职位:[]
};
}
componentDidMount(){
让自我=这个;
axios.get()http://localhost:8080/list')
.then(功能(数据){
控制台日志(数据);
self.setState({post:data});
});
}
render(){
返回(
{JSON.stringify(this.state.post.data)}
);
}
}
导出默认Premontessori;
您的数据是一个数组
。您可以简单地使用map在数组的所有元素上循环
,然后使用dot
符号或括号
符号访问javascript对象
的各个元素
class Premontessori扩展了React.Component{
建造师(道具){
超级(道具);
这个州={
职位:[]
};
}
componentDidMount(){
风险值数据=[{
“_id”:“588b3731d4428e2ff05ddefa”,
“Id”:50,
“名称”:“维斯鲁斯”,
“年龄”:11岁
}, {
“_id”:“588b45df255e323de55ac333”,
“Id”:51,
“名称”:“维斯鲁斯”,
“年龄”:11岁
}]
this.setState({post:data});
}
render(){
返回(
{this.state.post.map(函数(项,索引){
返回(
{item.\u id}
{item.Id}
{item.Name}
{item.Age}
)
})
}
)
}
}
ReactDOM.render(,document.getElementById(“app”)代码>
我正在从ComponentDidmount中的服务器获取数据,但没有在其中声明任何数据。ComponentDidmount(){let self=this;axios.get(')。然后(function(data){console.log(data);self.setState({post:data});}我理解,仅出于演示目的,我定义了它。正如您所见,我只是映射了this.state.post以及您访问它的方式。您可以忽略我在componentDidMount函数中所做的任何更改