如何在React Native中呈现JSON对象中的所有项目?

时间:2019-04-10 11:07:11

标签: react-native jsx

我需要在react native视图中呈现json对象中的所有项目,但是其中一些不在数组中并且不是单个项目。

这是我的api:

{
"meta": {
    "code": 200,
    "message": "success",
    "errors": []
},
"data": {
    "users": [
        {
            "userinfo": {
                "username": "hesamsameni",
                "fullname": "hesam sameni",
                "photo": "https://aaa.com/img/hesamsameni.png",
                "skills": {
                    "html": "HTML",
                    "css": "CSS",
                    "javascript": "JAVASCRIPT",
                    "react_native": "REACT NATIVE"
                }
            }
        }
    ]
}
}

这是我的渲染方法:

  render() {
return (
  <View>
    <FlatList
      data={this.state.dataSource}
      keyExtractor={(item, index) => index.toString()}
      renderItem={({item}) => 
       <View>
         <Text>{item.fullname}</Text>
         <Image source={{uri: item.photo}}/>
         <Text>Skills:</Text>
         // I need to render all the skills here
       </View>
    }></FlatList>
   </View>

1 个答案:

答案 0 :(得分:1)

你可以试试吗?

lapsList(datas) {
    const keys = Object.keys(datas); // Get all keys from dictionary

    return keys.map((key) => {
      return (
        <Text>{datas[key]}</Text>
      )
    })
}

render() {
return (
  <View>
    <FlatList
      data={this.state.dataSource}
      keyExtractor={(item, index) => index.toString()}
      renderItem={({item}) => 
       <View>
         <Text>{item.fullname}</Text>
         <Image source={{uri: item.photo}}/>
         <Text>Skills:</Text>

         {this.lapsList(item.skills)}
         // I need to render all the skills here
       </View>
    }></FlatList>
   </View>