json - React-Native:在 ListView 中显示 JSON 数据

标签 json listview download react-native

我想在 ListView 中显示 JSON-Data。问题是,JSON 数据包含字典。
在一行中,我想显示“Gattung”、“ab”和“bis”。
我无法在 ListView 中显示以下 JSON 数据:

[
{
    "Gattung": "ICE",
    "Zugummer": 26,
    "ab": "Regensburg Hbf",
    "bis": "Hanau Hbf",
    "Wochentag": "Fr",
    "Zeitraum": ""
},
{
    "Gattung": "ICE",
    "Zugummer": 27,
    "ab": "Frankfurt(Main)Hbf",
    "bis": "Regensburg Hbf",
    "Wochentag": "So",
    "Zeitraum": ""
},
{
    "Gattung": "ICE",
    "Zugummer": 28,
    "ab": "Regensburg Hbf",
    "bis": "Würzburg Hbf",
    "Wochentag": "Fr",
    "Zeitraum": ""
},
{
    "Gattung": "ICE",
    "Zugummer": 35,
    "ab": "Hamburg Hbf",
    "bis": "Puttgarden",
    "Wochentag": "tgl.",
    "Zeitraum": "25.06. - 04.09."
},
{
    "Gattung": "ICE",
    "Zugummer": 36,
    "ab": "Puttgarden",
    "bis": "Hamburg Hbf",
    "Wochentag": "tgl.",
    "Zeitraum": "25.06. - 04.09."
}
]

这是我现在的代码:
 var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});


var MainView = React.createClass ({





  getInitialState() {

     return {

        jsonURL: 'http://demo.morgenrot-wolf.de/qubidu/test.json',
        dataSource: ds.cloneWithRows(['row 1', 'row 2']),
     }
   },



   componentDidMount(){

      this.loadJSONData();

   },



   loadJSONData() {

     fetch(this.state.jsonURL, {method: "GET"})
     .then((response) => response.json())
     .then((responseData) =>
     {
          for (var i = 0; i < responseData.length; i++)
          {


              this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseData[i]) });
          }

     })
     .done(() => {


     });


   },




  render() {
    return (

      <View style={styles.container}>

         <ListView
              dataSource={this.state.dataSource}
              renderRow={(rowData) => <Text>{rowData}</Text>}
            />
      </View>

    );
  }
});

最佳答案

rowData 是一个对象,因此列表的 renderRow 属性应该类似于

renderRow: function(rowData) {
  return (
    <View style={styles.row}>
      <Text>{rowData.Gattung}</Text>
      <Text>{rowData.ab}</Text>
      <Text>{rowData.bis}</Text>
    </View>
  );
}

在循环内调用 setState 也是个坏主意。如果reponseData是一个数组,this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseData)});应该就够了。

检查此示例:https://rnplay.org/apps/4qH1HA

关于json - React-Native:在 ListView 中显示 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38589578/

相关文章:

javascript - 为什么 onChange 回调中 $scope.data 的更改不会重新绘制 chart.js?

javascript - Kendo Grid ,根据条件在详细网格中显示不同的模板

jquery - JSON.parse() 期间 JSON 中出现意外标记

c# - 在 ListView 中的图像上使用点击手势

R - 从原始数据集中下载子集

javascript - 使用 JQuery 触发浏览器下载

json - 将递归 Json 映射到类 Flutter

android - 使用动态加载的图像滚动 ListView 会混合图像顺序

WPF ListViewItem.IsMouseOver 不会为整行返回 True

java - 文件下载时返回字节数组或 servlet 输出流之间有区别吗