在React Native中渲染嵌套的json对象

时间:2018-01-14 08:41:26

标签: json react-native

在我的render()方法中,我需要解析嵌套的json对象。请参阅下面的render和json结构部分。 我使用{params.name_last}访问姓氏。 我将如何访问团队下的项目,例如team.name_first



render() {
        let { params } = this.props.navigation.state
        <Text>{params.name_last}</Text>
}
&#13;
&#13;
&#13;

&#13;
&#13;
[
	{
		"id": 1,
		"name_first": "Name first 1",
		"name_middle": "",
		"name_last": "Name last 1",
		"name_suffix": "",
		"phone": "888-8888",
		"fax": "888-8888",
		"updated_at": "2015-11-02T21:42:42.000Z",
		"team": [
			{
				"id": 16,
				"name_first": "aaa",
				"name_middle": "",
				"name_last": "bbb",
				"name_suffix": ""
			},
			{
				"id": 28,
				"name_first": "aaa",
				"name_middle": "",
				"name_last": "bbb",
				"name_suffix": ""
			},
			{
				"id": 29,
				"name_first": "aaa ",
				"name_middle": "",
				"name_last": "bbb",
				"name_suffix": ""
			}
		]
	}
 ]
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

由于team是一个数组,您需要访问数组中的特定条目,或者迭代整个数组。

要访问嵌套数组条目中的特定属性(假设您希望对象位于索引i):

params.team[i].name_first

创建名字数组:

params.team.map(x => x.name_first)