【发布时间】:2018-08-14 01:42:05
【问题描述】:
我需要一些帮助来渲染 jsx 中的 json 在 react native 中。我从服务器获得的响应是对象,所以我如何循环它们?我从服务器得到一个有效的响应,但我得到了map 方法的问题,因为我认为 map 方法仅适用于数组。所以请帮我这样做。以下是 json 对象
JSON
{
"content": "<p><strong>Preliminary Exam; MCQ –1</strong></p>\n",
"meta": {
"access": 1,
"status": 0,
"progress": 0,
"marks": 0,
"max": 60,
"questions": [
{
"type": "single",
"hint": "",
"explanation": "",
"content": "<p>Question Statement :</p>\n<ol>\n<li>The is question:</li>\n</ol>\n",
"options": [
"(a).Answer1",
"(b).Answer2 ",
"(c).Answer3 ",
"(d).Answer4 "
],
"correct": "4",
"marks": 4,
"user_marks": 0,
"status": 0,
"marked": null,
"auto": 1
},
{
"type": "single",
"hint": "",
"explanation": "",
"content": "<p>2.Question 2</p>\n",
"options": [
"(a) one ",
"(b) two ",
"(c) three ",
"(d) four"
],
"correct": "2",
"marks": 4,
"user_marks": 0,
"status": 0,
"marked": null,
"auto": 1
}
]
}
}
我试过如下
示例代码
state = { details: [] };
componentWillMount() {
AsyncStorage.getItem('userdetail').then((value) => {
console.log(value);
fetch('https://www.mywebsite.com', {
method:'GET',
headers: {
'Authorization': value
}
})
.then((response) => response.json())
.then((responseData) =>
this.setState({
details:responseData.meta.questions
})
);
});
}
render() {
console.log(this.state);
return (
this.state.details.map( a =>
<Card>
<CardSection>
<Text>{a.questions.content}</Text>
</CardSection>
</Card>
);
}
但出现错误this.state.details.map 不是函数?我的渲染方法有问题吗?请帮忙。
【问题讨论】:
标签: json react-native rendering jsx