【问题标题】:ReactNative - Parsing json file and display dataReact Native - 解析 json 文件并显示数据
【发布时间】:2017-03-16 07:15:16
【问题描述】:
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: '',
    textinput:'',
  }
   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) => {
        console.error(error);
        })
        .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)})

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

       .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>

                <Text>{this.state.data}</Text>  
            </View>
    );
    }
}


module.exports = Temp;

我正在开发一个 android 应用程序,我需要从 web 服务获取数据,即 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": "121"
},
  {
"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": "122"
},
  {
"entryDate": "03/02/2017",
"entryDateTime": "03/02/2017 2:00 AM",
"entryTime": "2:00 AM",
"systol": "123"
}
]
}

这是我的数据的样子。

我可以像这样显示

{"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": "121"
},{"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": "122"},{"entryDate": 03/02/2017","entryDateTime": "03/02/2017 2:00 AM","entryTime": "2:00 AM", "systol": "123"}]}

但我想这样显示,只有 entryDate 和 systol

entryDate:03/02/2017
systol:120
entryDate:03/02/2017
systol:121
entryDate:03/02/2017
systol:122
entryDate:03/02/2017
systol:123

请帮我解决这个问题。谢谢

【问题讨论】:

    标签: json parsing react-native fetch


    【解决方案1】:

    将您的渲染函数替换为以下内容:

    render() {
    
      const { List: list } = this.state.data
      const renderList = list && list.map(({entryDate, systol},index) => {
        return (
          <View key={index}>
            <Text>{entryDate}</Text>
            <Text>{systol}</Text>
          </View>
        )
      })
      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>
    
          {renderList}
    
        </View>
      );
    
    }
    

    您需要做的就是映射列表并从正在映射的项目中选择 entryDate 和 systol。然后根据当前数据项(entryDate, systol)告诉 React 需要渲染什么。

    【讨论】:

    • 我越来越喜欢“未定义不是一个对象(评估'list.map')”
    • 是 url 必须以 .json 结尾还是什么??
    • 我不知道为什么它没有显示任何东西,正如你所说你可以显示{"List":[{"entryDate": "03/02/2017","entryDateTime":"03/02/2017 2:00 AM","entryTime": "2:00 AM","systol": "120"}, ...... 所以上面的代码应该只显示entryDate 和systol。我不会说问题出在 url。
    • 是否可以创建一个本地 .josn 文件并移动其中的所有数据,然后导入该 .json 文件并显示数据?
    • 非常感谢:) 它的工作.. 90% 已完成。但我收到警告
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多