从React native中的JSON列表中提取数据

时间:2017-03-14 07:29:40

标签: json parsing react-native fetch

我想解析一个JSON文件并在屏幕上显示一些数据。我可以使用以下代码显示所有数据,但我只想显示entryDatesysol。是否可以在_onPressButtonGET()中使用for循环并仅在那里显示数据?

       import React, { Component } from 'react';
        import {
          AppRegistry,
          StyleSheet,
          Text,
          View,
          Navigator,
          TouchableOpacity,
          Image,
          TouchableHighlight,
          Alert,
          TextInput
        } from 'react-native';
        import Button from 'react-native-button'
        import {Actions} from 'react-native-router-flux'
        import Home from './Home'

        export class Temp extends Component{
        constructor(props) {
          super(props);
        this.state = {
            data: '',
            data1:'',
            textinput:'',
            entryDate:[]

          }
           state={
                    shouldShow: false
                }
        }

            componentDidMount(){
            this._onPressButtonGET();
          } 

              _onPressButtonPOST(){
                fetch(URL, {
                    method: "POST",  
                     headers: { 
                         'Accept': 'application/json',
                         'Content-Type': 'application/json',
                     },
                    body: JSON.stringify({
                        "entryDate":"3/2/2017 2:00 AM", 
                        "systol": this.state.textinput,
                        "mobileType":"ANDROID",
                        "userName":"menutest",

                       })})
                .then((response) => response.json())
                .then((responseData) => {
                    Alert.alert(                              
                        "Blood pressure data",
                        "Blood pressure data - " + JSON.stringify(responseData)
                    )
                }).catch((error) => {
                    Alert.alert('problem while adding data');
                })
                .done(); 
            }

            _onPressButtonGET(){
                fetch(url, {
                    method: "POST",
                     headers: {
                         'Accept': 'application/json',
                         'Content-Type': 'application/json',
                     },
                    body: JSON.stringify({"mobileType":"ANDROID","userName":"menutest"})})
                .then((response) => response.json())
                .then((responseData) => {


                        this.setState({ data : JSON.stringify(responseData) })
data["list"].map(d => this.state.entryDate.push(d.entryDate));


                        this.setState({ entryDate: jsonResponse.entryDate, systol: responseData.systol })


                    }).catch((error) => {
                    Alert.alert('problem while getting data');
                })
               .done();
            }
            render(){
                return(

                    <View>
                        <TouchableHighlight onPress={this._onPressButtonPOST.bind(this)}>
                            <Text>Add</Text> 
                        </TouchableHighlight>

                    <TouchableOpacity style= {{left:300,top:-20, }}
         onPress={()=>{ this.setState({ shouldShow: !this.state.shouldShow })}}
        ><Text>Edit</Text></TouchableOpacity>

        {this.state.shouldShow ? <TextInput placeholder='systol' 
                    onChangeText={(text) => this.setState({textinput: text})}
                   /> : null}

                         <TouchableHighlight onPress={this._onPressButtonGET.bind(this)}>
                            <Text>show</Text>
                           </TouchableHighlight>
this.state.entryDate.map( d => (<Text>{d}</Text>));
                           <Text>{this.state.entryDate}</Text> //not displaying anythong
                       <Text>{this.state.data && JSON.parse(this.state.data)['entryDate']}</Text> //not displaying anything
                        <Text>hello{this.state.data}</Text> //printing all data 
                    </View>
            );
            }
        }


        module.exports = Temp;

JSON:

{
  "List": [
    {
      "entryDate": "03/02/2017",
      "entryDateTime": "03/02/2017 2:00 AM",
      "entryTime": "2:00 AM",
      "systol": "120"
    },
    {
      "entryDate": "03/02/2017",
      "entryDateTime": "03/02/2017 2:00 AM",
      "entryTime": "2:00 AM",
      "systol": "120"
    }
  ]
}

2 个答案:

答案 0 :(得分:4)

首先将entryData初始化为数组

this.state = {
   entryDate: [],
}
_onPressButtonGET()中的

使用

data["List"].map(d => this.state.entryData.push(d.entryDate));

在渲染中使用map来显示所有类似的数据

this.state.entryDate.map( d => (<Text>{d}</Text>));

答案 1 :(得分:1)

    onPressButtonGET(){
                fetch(url, {
                    method: "POST",
                     headers: {
                         'Accept': 'application/json',
                         'Content-Type': 'application/json',
                     },
                    body: JSON.stringify({"mobileType":"ANDROID","userName":"menutest"})})
    }
        .then(response => {
            this.setState({entryDate:response.data})
        });


renderEntryDate() {
    this.state.entryDate.map((item)=>{
      <Text>{item.entryDate}</Text>
    })
    }

render() {
    return (
      <View>
      <Button style= {{
                  backgroundColor: '#6656B4',
                  width: 200,
                  height: 40,
                 }
                onPress={() => this.onPressButtonGET()}
                title="Get Weather"
                color="#841584"
        />
        {this.renderEntryDate()}
      <View>
    );
}