[英]How do I receive a JSON object from PHP backend and convert to JavaScript array for frontend
I have a backend in php that is returning a json object to a React frontend.我在 php 中有一个后端,它将 json object 返回到 React 前端。 The object arrives in this format
object 以这种格式到达
{data: Array(401), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config: {url: "http://localhost/index.php", method: "post", headers: {…}, transformRequest:
Array(1), transformResponse: Array(1), …}
data: Array(401)
[0 … 99]
0: {id: "191", name: "Thunder", description: null, created: "2012-10-04 12:36:29", slug: "thunder", …}
1: {id: "95", name: "Break", description: null, created: "2010-03-26 13:19:11", slug: "break", …}
etc.等等
I want to take this object and grab the data array and then loop the array.我想拿这个 object 抓取数据数组然后循环数组。 This is what I have so far:
这是我到目前为止所拥有的:
import React from 'react';
import axios from 'axios';
class Container extends React.Component{
constructor(props) {
super(props);
this.state = {
data: [],
};
}
componentDidMount() {
axios({
method: 'post',
url: 'http://localhost/index.php',
timeout: 4000,
})
.then(response => {
this.setState({data:response})
})
.catch(error => console.error('timeout exceeded'))
}
render() {
const {data} = this.state;
const items = Object.values(data).map(function(num) {
return num
});
return (
<div>
{items}
</div>
)
}
}
export default Container;
This throws an error of这会引发错误
Uncaught Error: Objects are not valid as a React child (found: object with keys {id, name, description, created, slug, image, tags}).
未捕获的错误:对象作为 React 子项无效(发现:object 键为 {id, name, description, created, slug, image, tags})。 If you meant to render a collection of children, use an array instead.
如果您打算渲染一组子项,请改用数组。
If I wrap the items[0] in JSON.stringify I get a string, but I want to convert the response object to an array so I can use data.map(item => etc)
.如果我将 items[0] 包装在 JSON.stringify 中,我会得到一个字符串,但我想将响应 object 转换为数组,以便我可以使用
data.map(item => etc)
。
Update:更新:
num = num = (401) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}
[0 … 99]
0: {id: "191", name: "Thunder", description: null, created: "2012-10-04 12:36:29", slug: "thunder", …}
1: {id: "95", name: "Break", description: null, created: "2010-03-26 13:19:11", slug: "break", …} etc etc
How about this?这个怎么样? Pass the array directly to the state instead of passing an object.
将数组直接传递给 state,而不是传递 object。 This way you can loop directly through the array instead of the object values.
这样,您可以直接循环遍历数组而不是 object 值。
import React from 'react';
import axios from 'axios';
class Container extends React.Component{
constructor(props) {
super(props);
this.state = {
data: [],
};
}
componentDidMount() {
axios({
method: 'post',
url: 'http://localhost/index.php',
timeout: 4000,
})
.then(response => {
this.setState({data:response.data})
})
.catch(error => console.error('timeout exceeded'))
}
render() {
const {data} = this.state;
const items = data.map(function(num) {
return num.id;
});
return (
<div>
{items}
</div>
)
}
}
export default Container;
ok so with some of the answers and a little more experimenting this is what I have and its a start to the rest of the solution好的,所以有了一些答案和更多的实验,这就是我所拥有的,它是解决方案 rest 的开始
I changed this line我改变了这条线
this.setState({data:response})
to至
this.setState({data:response.data})
and changed the render to this并将渲染更改为此
render() {
const {data} = this.state
let result = [];
Object.values(data).map(num =>
result.push(num)
);
return (
<div>
{result.map(tile =>
`<div><img src='${tile.image}'/></div>`
)}
</div>
)
}
Use .setState()
.使用
.setState()
。 It will make react re-render the page.它会使反应重新渲染页面。 Also, what's the value of
num
in your function?另外,您的 function 中的
num
值是多少? Might want to check that可能想检查一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.