【发布时间】:2020-02-13 03:35:31
【问题描述】:
使用 React-Native:
我将从我所拥有的开始..
json文件示例代码:
并且使用的任何“...”仅用于我的与问题无关的字符串值,看起来会分散注意力
[
{
"id": "question1"
"label": "..."
"option": [ { "order": 1, "name": "..."},
{ "order": 2, "name": "..."},
{ "order": 3, "name": "..."}
]
},
{
"id": "question2"
"label": "..."
"option": [ { "order": 1, "name": "..."},
{ "order": 2, "name": "..."},
{ "order": 3, "name": "..."}
]
},
{
"id": "question3"
"label": "..."
"option": [ { "order": 1, "name": "..."},
{ "order": 2, "name": "..."},
{ "order": 3, "name": "..."}
]
},
and so on....
在类下渲染代码
"data1" 就是我导入时调用的 json 文件,例如:import data1 from "../../...."
render(){
return(
<View
<FlatList
data={data1}
showsVerticalScrollIndicator={false}
renderItem={this.renderItem}
keyExtractor={(item,index) => index.toString()}
/>
</View>
);
}
渲染项代码
renderItem = ({item}) => {
return(
<View
<TouchableOpacity>
<Text {item.id} </Text>
</TouchableOpacity>
<TouchableOpacity>
<Text {item.label} </Text>
</TouchableOpacity>
<TouchableOpacity>
<Text {item.option} </Text> /* here is where I get the error because option is an object! */
</TouchableOpacity>
<Text {item.name} </Text> /* this doesn't work either obviously */
</TouchableOpacity>
我知道我无法访问选项,因为它是一个对象,但我希望能够访问其中的键(顺序和名称)
item.id 和 item.label 有效,因为它们不在对象“选项”中。我似乎不知道如何访问要在应用程序中使用的密钥。
作为参考,它是一个调查应用程序(因此是 TouchableOpacity),“标签”是用户的问题,对象“选项”及其“名称”是用户可以点击的各种答案。
我想我可能需要映射它,但我不太确定该怎么做。
【问题讨论】:
标签: javascript json reactjs react-native