Javascript 如何在react native中从GET请求解析JSON数组?

Javascript 如何在react native中从GET请求解析JSON数组?,javascript,react-native,Javascript,React Native,我从我的react本地代码发出GET请求调用,GET请求的响应如下所示- [ { "id": 1, "title": "homework", "detail": "nothing details", "status": "1", "url": "www.google.com", "mail": "a@a.com", "phone": "0171551223366",

我从我的react本地代码发出GET请求调用,GET请求的响应如下所示-

[
    {
        "id": 1,
        "title": "homework",
        "detail": "nothing details",
        "status": "1",
        "url": "www.google.com",
        "mail": "a@a.com",
        "phone": "0171551223366",
        "category": "2",
        "timestamp": 12.3
    },
    {
        "id": 2,
        "title": "homework",
        "detail": "nothing details",
        "status": "1",
        "url": "www.google.com",
        "mail": "a@a.com",
        "phone": "0171551223366",
        "category": "2",
        "timestamp": 12.3
    }
]
在这种情况下,如果它是一个简单的json响应,解析就没有任何问题,但在这种情况下,这是一个数组,我不知道如何获取它们并将它们保存在数组中

这里我给出了我的代码-

import React from 'react';
import { StyleSheet, Text, View, Image, AsyncStorage, ActivityIndicator } from 'react-native';
import {Icon, Button, Container, Header, Content, Left} from 'native-base';
import CustomHeader from './CustomHeader';

let jwt=''
 class NoteMeHome extends React.Component {


   state = {
      getValue: '',
      dataSource:[],
      isLoading: true
    }


  componentDidMount() {
    AsyncStorage.getItem("token").then(value => {
      console.log(value);
      jwt = value;

      this.setState({
        getValue: value,
      });
    });

    const url = 'my url';
    fetch(url, {
      method: 'GET',
      headers: new Headers({
        'Content-Type' : 'application/json',
        'token': 'abcd',
        'jwt': jwt
      })
    })
    .then((response)=> response.json() )
    .then((responseJson) => {
      console.log('####:'+responseJson.title)
      this.setState({
        dataSource: responseJson,
        isLoading: false
      })
    })
    .catch((Error) => {
      console.log(Error)
    })


  }

  static navigationOptions = ({navigation}) => ({
    title: "Home",
    headerLeft: <Icon name="ios-menu" style={{paddingLeft:10}}
    onPress={()=>navigation.navigate('DrawerOpen')}/>,

    drawerIcon: 

    <Image source={{uri: 'https://png.icons8.com/message/ultraviolet/50/3498db'}}
            style={styles.icon}
    />
  })



  render() {
    const { getValue } = this.state;

    return(
      this.state.isLoading
      ?
      <View style={{flex:1, justifyContent:'center', alignItems:'center'}}>
        <ActivityIndicator size="large" color="#330066" animating/>
      </View>
      :
      <Container>
        <CustomHeader
          title="Home"
          drawerOpen={()=>this.props.navigation.navigate('DrawerOpen')}
        />
        <Content contentContainerStyle={{flex:1, alignItems:'center', 
        justifyContent:'center', padding:10}}>
        <Button full onPress={()=> this.props.navigation.navigate('Settings')}>
          <Text style={{color:'white'}}>Go To Settings</Text>
        </Button>
        <Text>{this.state.getValue}</Text>
        </Content>
      </Container>
    )
  }



}



const styles = StyleSheet.create({
  icon:{
    height: 24,
    width: 24
  }
})
export default NoteMeHome;
从“React”导入React;
从“react native”导入{样式表、文本、视图、图像、异步存储、ActivityIndicator};
从“本机基础”导入{图标、按钮、容器、标题、内容、左侧};
从“/CustomHeader”导入CustomHeader;
让jwt=''
类NoteMeHome扩展了React.Component{
状态={
getValue:“”,
数据源:[],
孤岛加载:正确
}
componentDidMount(){
AsyncStorage.getItem(“令牌”)。然后(值=>{
console.log(值);
jwt=值;
这是我的国家({
getValue:value,
});
});
const url='我的url';
获取(url{
方法:“GET”,
标题:新标题({
“内容类型”:“应用程序/json”,
“令牌”:“abcd”,
“jwt”:jwt
})
})
.then((response)=>response.json())
.然后((responseJson)=>{
console.log(“#####:”+responseJson.title)
这是我的国家({
数据来源:responseJson,
孤岛加载:false
})
})
.catch((错误)=>{
console.log(错误)
})
}
静态导航选项=({navigation})=>({
标题:“家”,
headerLeft:navigation.navigate('drawerropen')}/>,
付款人:
})
render(){
const{getValue}=this.state;
返回(
此.state.isLoading
?
:
this.props.navigation.navigate('drawerropen')}
/>
this.props.navigation.navigate('Settings')}>
转到“设置”
{this.state.getValue}
)
}
}
const styles=StyleSheet.create({
图标:{
身高:24,
宽度:24
}
})
导出默认NoteMeHome;

因此,如果有人能帮助我编写这段代码,并建议如何将json响应中的数据保存到数组中,那将非常好。

它应该可以工作。否则,您可以这样做

for(变量i=0;i}
responseJson
已经是一个数组。不清楚你的具体问题是什么对不起,我以前不理解这一点。但是在您的建议之后,我编写了console.log(response[0].title)&从JSON响应中获得了一些值。所以,谢谢