1

I want to parse a JSON file and display some data on the screen. I am able to display all the data using the following code, but I want to display only entryDate and sysol. Is it possible to use a for loop in _onPressButtonGET() and display data there only?

       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"
    }
  ]
}
9
  • 1
    change data["list"] to data["List"] Commented Mar 14, 2017 at 8:09
  • still same error. Commented Mar 14, 2017 at 8:11
  • use { this.state.entryDate.map( d => (<Text>{d}</Text>)) } Commented Mar 14, 2017 at 8:16
  • now no error, but control is going to .catch(error).. block Commented Mar 14, 2017 at 8:32
  • so it's a problem with your api call , check your request and your server. Commented Mar 14, 2017 at 8:33

2 Answers 2

4

first initialize entryData as array

this.state = {
   entryDate: [],
}

in _onPressButtonGET() use

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

in render use map to display all data like that

this.state.entryDate.map( d => (<Text>{d}</Text>));
Sign up to request clarification or add additional context in comments.

3 Comments

you mean entryDate?
sorry yup entryDate
error like cant find variable:d. this error is in render
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>
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.