[英]How to get objects of objects in react native?
My JSON data is我的 JSON 数据是
[
{
id: 51,
name: 'Boat Neck Blouse',
image: {
id: 669,
date_created: '2018-08-27T10:05:39',
date_created_gmt: '2018-08-27T10:05:39',
date_modified: '2018-08-27T10:05:39',
date_modified_gmt: '2018-08-27T10:05:39',
src:
'https://dreamdesigners.rkhomeappliances.co.in/wp-content/uploads/2018/08/boatneck.jpg',
title: 'boatneck',
alt: '',
},
menu_order: 0,
},
];
I can get the name, id, and all but can't get src of image object
.我可以获取名称、ID 以及所有但无法获取
src of image object
。 What I tried is我试过的是
state = { data: [] };
fetchData = async () => {
const response = await fetch('mywebsite.com/json');
const posts = await response.json();
this.setState({ data: posts });
};
componentDidMount() {
this.fetchData();
}
render() {
return (
<Container>
<ScrollView style={{ backgroundColor: '#eeeeee' }}>
<View>
<FlatList
contentContainerStyle={styles.list}
numColumns={2}
data={this.state.data}
keyExtractor={(x, i) => i.toString()}
renderItem={({ item }) => (
<TouchableHighlight
style={{ width: '50%' }}
underlayColor="white">
<View style={styles.view}>
<Text>{item.image.src}</Text>
<Text style={styles.text}>{item.name}</Text>
</View>
</TouchableHighlight>
)}
/>
</View>
</ScrollView>
</Container>
);
}
I just tried to display the src of the image but I get the below error.我只是尝试显示图像的 src,但出现以下错误。
TypeError: undefined is not an object (evaluating '_this3.item.image')
How to get the src from the image object?如何从图像对象中获取 src? I'm a newbie in react native and react js.
我是 react native 和 react js 的新手。 Please help me to get the image src.
请帮我获取图像 src。
您必须将 this.item.image 替换为 item.image.src
[
{
id: 51,
name: 'Boat Neck Blouse',
image: {
id: 669,
date_created: '2018-08-27T10:05:39',
date_created_gmt: '2018-08-27T10:05:39',
date_modified: '2018-08-27T10:05:39',
date_modified_gmt: '2018-08-27T10:05:39',
src:
'https://dreamdesigners.rkhomeappliances.co.in/wp-content/uploads/2018/08/boatneck.jpg',
title: 'boatneck',
alt: '',
},
menu_order: 0,
},
];
This is the way if you want to access the date_created
(As an Example)如果您想访问
date_created
(作为示例),这就是方法
Try this尝试这个
(Let's say the data is assigned to variable data
) As you can see the JSON starts with an array so you will have to access the content in the array. (假设数据分配给变量
data
)如您所见,JSON 以数组开头,因此您必须访问数组中的内容。 In this case, the array only has 1 content.在这种情况下,数组只有 1 个内容。 So, if you access
data[0]
, you will get所以,如果你访问
data[0]
,你会得到
{
id: 51,
name: 'Boat Neck Blouse',
image: {
id: 669,
date_created: '2018-08-27T10:05:39',
date_created_gmt: '2018-08-27T10:05:39',
date_modified: '2018-08-27T10:05:39',
date_modified_gmt: '2018-08-27T10:05:39',
src:
'https://dreamdesigners.rkhomeappliances.co.in/wp-content/uploads/2018/08/boatneck.jpg',
title: 'boatneck',
alt: '',
},
menu_order: 0,
};
then if you want to access date_created
in image
key, it will be data[0].image.date_created
那么如果你想在
image
键中访问date_created
,它将是data[0].image.date_created
Hope this helps you to understand accessing JSON data希望这可以帮助您了解访问 JSON 数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.