将 JSON 显示到具有多个 json 数组的列表视图中

史密斯夏洛克

我正在开发一个本机应用程序,我想显示包含数组的 json 对象。我想知道如何将该数据显示到列表视图中。我的问题是我无法显示所有数组的内容,它只显示第一个数组的数据索引而不是整个数组列表

以下是我将要使用的 json:-

{
  "response": {
    "airlinedetails": [
      {
        "airlinecode": "9W",
        "airlinelogopath": "9W.png",
        "totalprice": "4478",
        "airlinename": "Jet Airways",
        "noofstops": "0",
        "nonstop": "Y",
        "airlineRecommend": "N",
        "noofflights": 6,
        "originaltp": "4449"
      },
      {
        "airlinecode": "SG",
        "airlinelogopath": "SG.png",
        "totalprice": "4511",
        "airlinename": "Spicejet",
        "noofstops": "0",
        "nonstop": "Y",
        "airlineRecommend": "N",
        "noofflights": 3,
        "originaltp": "4483"
      },      {
        "airlinecode": "UK",
        "airlinelogopath": "UK.png",
        "totalprice": "6319",
        "airlinename": "Vistara",
        "noofstops": "0",
        "nonstop": "Y",
        "airlineRecommend": "N",
        "noofflights": 1,
        "originaltp": "6280"
      },
      {
        "airlinecode": "AI",
        "airlinelogopath": "AI.png",
        "totalprice": "4499",
        "airlinename": "Air India",
        "noofstops": "0",
        "nonstop": "Y",
        "airlineRecommend": "N",
        "noofflights": 17,
        "originaltp": "4470"
      },
      {
        "airlinecode": "6E",
        "airlinelogopath": "6E.png",
        "totalprice": "4852",
        "airlinename": "Indigo Airlines",
        "noofstops": "0",
        "nonstop": "Y",
        "airlineRecommend": "N",
        "noofflights": 6,
        "originaltp": "4820"
      }`enter code here`
    ],





Tell me how to display it using map function as renderrow is called only once ,how to iterate through all those arrays of airlindetails. How to loop through JSON and print all the values and not just index 0 values of airlinedetails




This is my react native code ,i have add for loop to iterate through json but it doesnt work like expected as it iterate through only through first index


import React, { Component } from 'react';
import { StyleSheet, View, ListView, Image, Text,AppRegistry } from 'react-native';

import data from './flights.json';

export default class Navin extends Component {

constructor(props) {
     super(props);
     var ds = new ListView.DataSource({
       rowHasChanged: (r1, r2) => r1 !== r2
     });

      this.state = {
        dataSource: ds.cloneWithRows(data),
    };
  }

  renderRow(record) {

    for(var i=0;i<5;i++){
     return (

      <View style={styles.row}>
        <View style={styles.info}>

          <Text style={styles.items}>{record.airlinedetails[i].airlinecode}         </Text>
          <Text style={styles.address}>{record.airlinedetails[i].airlinecode}</Text>
        </View>
        <View style={styles.total}>
          <Text style={styles.date}>{record.airlinedetails[i].airlinecode}</Text>
          <Text style={styles.price}>${record.airlinedetails[i].airlinecode}   </Text>
        </View>
      </View>

       );
        continue;
  }      
    }
     render() {
      return (
       <View style={styles.mainContainer}>
        <Text style={styles.title}>Flights</Text>
        <ListView
          dataSource={this.state.dataSource}
          renderRow={this.renderRow}
        />
      </View>
      );
     }
    }

    const styles = StyleSheet.create({
      mainContainer: {
    flex: 1,
    backgroundColor: '#fff',
  },
  title: {
    backgroundColor: '#0f1b29',
    color: '#fff',
    fontSize: 18,
    fontWeight: 'bold',
    padding: 10,
    paddingTop: 40,
    textAlign: 'center',
  },
  row: {
    borderColor: '#f1f1f1',
    borderBottomWidth: 1,
    flexDirection: 'row',
    marginLeft: 10,
    marginRight: 10,
    paddingTop: 20,
    paddingBottom: 20,
  },
  iconContainer: {
    alignItems: 'center',
    backgroundColor: '#feb401',
    borderColor: '#feaf12',
    borderRadius: 25,
    borderWidth: 1,
    justifyContent: 'center',
    height: 50,
    width: 50,
  },
  icon: {
    tintColor: '#fff',
    height: 22,
    width: 22,
  },
  info: {
    flex: 1,
    paddingLeft: 25,
    paddingRight: 25,
  },
  items: {
    fontWeight: 'bold',
    fontSize: 16,
    marginBottom: 5,
  },
  address: {
    color: '#ccc',
    fontSize: 14,
  },
  total: {
    width: 80,
  },
  date: {
    fontSize: 12,
    marginBottom: 5,
  },
  price: {
    color: '#1cad61',
    fontSize: 25,
    fontWeight: 'bold',
  },
});

AppRegistry.registerComponent('Navin', () => Navin);
托比亚斯·林斯

您应该cloneWithRows使用数组而不是对象调用 。

renderRow 将与数组的每个条目一起调用-> 您不需要为此编写循环。

而不是data尝试使用以下命令:

dataSource: ds.cloneWithRows(data.response.airlinedetails),

现在你需要renderRow像这样调整你的方法:

renderRow(record) {
  return (
    <View style={styles.row}>
      <View style={styles.info}>
        <Text style={styles.items}>{record.airlinecode}</Text>
        <Text style={styles.address}>{record.airlinecode}</Text>
      </View>
      <View style={styles.total}>
        <Text style={styles.date}>{record.airlinecode}</Text>
        <Text style={styles.price}>${record.airlinecode}</Text>
      </View>
    </View>
  );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将数据搜索到json列表视图中?

来自分类Dev

如何存储多个JSON数组的项目并在列表视图中显示它?

来自分类Dev

在Xamarin android中将json数组响应绑定到列表视图中的列表行

来自分类Dev

JSON解析到具有多个对象的Android的列表视图

来自分类Dev

JSON解析到具有多个对象的Android的列表视图

来自分类Dev

在列表视图中显示JSON数据

来自分类Dev

如何从SharedPreferences将数据发送到PHP脚本并在列表视图中显示JSON?

来自分类Dev

使用带有restful api的json parsom将数据填充到列表视图中

来自分类Dev

AngularJS没有在列表视图中显示JSON数据

来自分类Dev

将具有对象列表的json绑定到模型

来自分类Dev

将具有对象列表的json绑定到模型

来自分类Dev

在视图中显示Json

来自分类Dev

在视图中显示Json

来自分类Dev

Laravel 4-将JSON解码到视图中

来自分类Dev

Laravel 4-将JSON解码到视图中

来自分类Dev

如何在列表视图中与其他号码一起显示我的 json 电话号码数组?

来自分类Dev

Android:获取并解析json数据到列表视图中

来自分类Dev

解析的JSON数据未显示在列表视图中

来自分类Dev

如何在列表视图中显示JSON对象?

来自分类Dev

如何在没有数组键的情况下完成将JSON数组解析为Android中的列表视图

来自分类Dev

如何在不预定义 JSON 结构的情况下将 JSON 响应显示到 MVC 视图中?

来自分类Dev

将数据从 json 对象列表视图传递到新活动

来自分类Dev

如何在水平列表视图中显示从Web服务检索到的Json文本和图像?

来自分类Dev

将具有多个值的 JSON 从 S3 复制到 Redshift

来自分类Dev

通过JSON将图像数组加载到tableview图像视图中

来自分类Dev

无法在列表视图中显示视图的一些JSON数据

来自分类Dev

从多个数组将值插入到列表视图中C#

来自分类Dev

将具有多个值的字符串列表解析为JSON

来自分类Dev

从列表视图中的JSON缓存图像?

Related 相关文章

  1. 1

    如何将数据搜索到json列表视图中?

  2. 2

    如何存储多个JSON数组的项目并在列表视图中显示它?

  3. 3

    在Xamarin android中将json数组响应绑定到列表视图中的列表行

  4. 4

    JSON解析到具有多个对象的Android的列表视图

  5. 5

    JSON解析到具有多个对象的Android的列表视图

  6. 6

    在列表视图中显示JSON数据

  7. 7

    如何从SharedPreferences将数据发送到PHP脚本并在列表视图中显示JSON?

  8. 8

    使用带有restful api的json parsom将数据填充到列表视图中

  9. 9

    AngularJS没有在列表视图中显示JSON数据

  10. 10

    将具有对象列表的json绑定到模型

  11. 11

    将具有对象列表的json绑定到模型

  12. 12

    在视图中显示Json

  13. 13

    在视图中显示Json

  14. 14

    Laravel 4-将JSON解码到视图中

  15. 15

    Laravel 4-将JSON解码到视图中

  16. 16

    如何在列表视图中与其他号码一起显示我的 json 电话号码数组?

  17. 17

    Android:获取并解析json数据到列表视图中

  18. 18

    解析的JSON数据未显示在列表视图中

  19. 19

    如何在列表视图中显示JSON对象?

  20. 20

    如何在没有数组键的情况下完成将JSON数组解析为Android中的列表视图

  21. 21

    如何在不预定义 JSON 结构的情况下将 JSON 响应显示到 MVC 视图中?

  22. 22

    将数据从 json 对象列表视图传递到新活动

  23. 23

    如何在水平列表视图中显示从Web服务检索到的Json文本和图像?

  24. 24

    将具有多个值的 JSON 从 S3 复制到 Redshift

  25. 25

    通过JSON将图像数组加载到tableview图像视图中

  26. 26

    无法在列表视图中显示视图的一些JSON数据

  27. 27

    从多个数组将值插入到列表视图中C#

  28. 28

    将具有多个值的字符串列表解析为JSON

  29. 29

    从列表视图中的JSON缓存图像?

热门标签

归档