Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.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
React Native:如何访问JSON对象中的数据?_Json_Reactjs_React Native - Fatal编程技术网

React Native:如何访问JSON对象中的数据?

React Native:如何访问JSON对象中的数据?,json,reactjs,react-native,Json,Reactjs,React Native,我想访问JSON对象内部的数据,但我不知道在这种情况下如何访问。 1.为什么我下面的例子不起作用? 2.在获取整个JSON数据之后,如何浏览数据以访问必要的元素?我甚至不知道该尝试什么,甚至不知道在代码中的什么地方这样做,因为我已经无法访问一个简单的数据 简短解释 以下工作: dataSource: responseJson.shosfc.weekday[7] renderItem={({item}) => <Text>{item.min}</Text>} JSO

我想访问JSON对象内部的数据,但我不知道在这种情况下如何访问。 1.为什么我下面的例子不起作用? 2.在获取整个JSON数据之后,如何浏览数据以访问必要的元素?我甚至不知道该尝试什么,甚至不知道在代码中的什么地方这样做,因为我已经无法访问一个简单的数据

简短解释 以下工作:

dataSource: responseJson.shosfc.weekday[7]
renderItem={({item}) => <Text>{item.min}</Text>}
JSON API:

第二个给出了一个错误:

TypeError: Cannot read property 'weekday' of undefined

This error is located at:

    in CellRenderer (at VirtualizedList.js:688)
    in RCTScrollContentView (at ScrollView.js:1007)
    in RCTScrollView (at ScrollView.js:1147)
    in ScrollView (at VirtualizedList.js:1081)
    in VirtualizedList (at FlatList.js:634)
    in FlatList (at App.js:42)
    in RCTView (at View.js:35)
    in View (at App.js:41)
    in FetchExample (at renderApplication.js:40)
    in RCTView (at View.js:35)
    in View (at AppContainer.js:98)
    in RCTView (at View.js:35)
    in View (at AppContainer.js:115)
    in AppContainer (at renderApplication.js:39)

renderItem
    injectUpdate.js:71:4
FlatList._this._renderItem
    FlatList.js:628:13
CellRenderer.render
    VirtualizedList.js:1741:20
CellRenderer.proxiedMethod
    createPrototypeProxy.js:44:29
下面是完整的代码

import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class FetchExample extends React.Component {

    constructor(props){
        super(props);
        this.state ={ isLoading: true}
    }

    // make an API call in the beginning
    componentDidMount(){
        return fetch('https://api.myjson.com/bins/wa759')
            .then((response) => response.json())
            .then((responseJson) => {

                this.setState({
                    isLoading: false,
                    dataSource: responseJson.shosfc.weekday[7]
                }, function(){

                });

            })
            .catch((error) =>{
                console.error(error);
            });
    }

    render(){

        if(this.state.isLoading){
            return(
                <View style={{flex: 1, padding: 50}}>
                    <ActivityIndicator/>
                </View>
            )
        }

        return(
            <View style={{flex: 1, paddingTop:50}}>
                <FlatList
                    data={this.state.dataSource}
                    renderItem={({item}) => <Text>{item.min}</Text>}
                    // keyExtractor={({id}, index) => id}
                />
            </View>
        );
    }
}
FlatList组件仅接受道具数据的数组

似乎responseJson.shosfc.weekday[7]是一个数组,而responseJson是一个对象

来源:

平面列表组件仅接受道具数据的数组

似乎responseJson.shosfc.weekday[7]是一个数组,而responseJson是一个对象

来源:

需要将数据作为对象列表。当您传递responseJson.shosfc.weekday[7]时就是这种情况:

[ {min:10,键入'h'}, {min:17,键入'h'}, {min:24,键入'ht'}, {min:30,键入'h'}, {min:35,键入'ht'}, {min:40,键入'h'}, {min:44,键入'h'}, {min:48,键入'h'}, {min:53,键入'ht'}, {min:56,键入:'h'} ] 但是,当您只是传递responseJson时,情况并非如此:

{ 证监会:{ 工作日:{ “7”:[数组], “8”:[数组], “9”:[数组], “10”:[数组], “11”:[数组], “12”:[数组], “13”:[数组], “14”:[数组], “15”:[数组], “16”:[数组], “17”:[数组], “18”:[数组], “19”:[数组], “20”:[数组], “21”:[数组], '22': [], '23': [], }, // [...] }, // [...] } 需要将数据作为对象列表。当您传递responseJson.shosfc.weekday[7]时就是这种情况:

[ {min:10,键入'h'}, {min:17,键入'h'}, {min:24,键入'ht'}, {min:30,键入'h'}, {min:35,键入'ht'}, {min:40,键入'h'}, {min:44,键入'h'}, {min:48,键入'h'}, {min:53,键入'ht'}, {min:56,键入:'h'} ] 但是,当您只是传递responseJson时,情况并非如此:

{ 证监会:{ 工作日:{ “7”:[数组], “8”:[数组], “9”:[数组], “10”:[数组], “11”:[数组], “12”:[数组], “13”:[数组], “14”:[数组], “15”:[数组], “16”:[数组], “17”:[数组], “18”:[数组], “19”:[数组], “20”:[数组], “21”:[数组], '22': [], '23': [], }, // [...] }, // [...] } 记录responseJson时看到什么?记录responseJson时看到什么?
import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class FetchExample extends React.Component {

    constructor(props){
        super(props);
        this.state ={ isLoading: true}
    }

    // make an API call in the beginning
    componentDidMount(){
        return fetch('https://api.myjson.com/bins/wa759')
            .then((response) => response.json())
            .then((responseJson) => {

                this.setState({
                    isLoading: false,
                    dataSource: responseJson.shosfc.weekday[7]
                }, function(){

                });

            })
            .catch((error) =>{
                console.error(error);
            });
    }

    render(){

        if(this.state.isLoading){
            return(
                <View style={{flex: 1, padding: 50}}>
                    <ActivityIndicator/>
                </View>
            )
        }

        return(
            <View style={{flex: 1, paddingTop:50}}>
                <FlatList
                    data={this.state.dataSource}
                    renderItem={({item}) => <Text>{item.min}</Text>}
                    // keyExtractor={({id}, index) => id}
                />
            </View>
        );
    }
}