Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类组件react native中的映射函数_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 类组件react native中的映射函数

Javascript 类组件react native中的映射函数,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我是一个新手,我正在尝试迭代一个json文件,希望通过使用map函数来渲染对象。 但是,它不显示对象。有人能告诉我我做错了吗?提前谢谢 这是我的密码: import React, { Component } from 'react'; import { View, Text } from 'react-native'; import stations from './test.json'; export default class Playlist extends Component {

我是一个新手,我正在尝试迭代一个json文件,希望通过使用map函数来渲染对象。 但是,它不显示对象。有人能告诉我我做错了吗?提前谢谢

这是我的密码:

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import stations from './test.json';



export default class Playlist extends Component {
    constructor(props) {
        super(props);
        this.state = {
            allStations: [stations],

        };

    }



    render() {
        return (

            <View>
                {this.state.allStations.map((item, index) => (
                    (
                        <View>
                            <Text>{item.firstName}</Text>
                        </View>
                    )
                ))}

            </View>




        )
    }
}
将“test.json”文件添加到资产文件夹中,并将其分配给index.js文件,这样当您有更多文件时,可以将其添加到资产下,并从单个位置导出


有关如何使用map()函数的详细信息

您需要分散站点
allStations:[…stations]
。此外,导入JSON的方式也不正确。如果我没有正确回答问题,请参阅下面的Plz评论,以便我可以更改我的答案,谢谢
[{
    "firstName": "Joe",
    "lastName": "Jackson",
    "gender": "male",
    "age": 28

   
 },
 {
    "firstName": "Brad",
    "lastName": "Brown",
    "gender": "male",
    "age": 45
   
   
 }
]