【问题标题】:How to implement for loop for JsonArray Data [duplicate]如何为 JsonArray 数据实现 for 循环 [重复]
【发布时间】:2018-03-23 10:25:25
【问题描述】:

您好,我无法对来自 nodejs 服务器的以下多维 JsonArray 数据进行循环。

只要有更多数据进入,即使 jsonarray 的索引值也会动态增加。请帮我实现for循环以动态获取数据。

我从服务器得到的响应是:-

[
    [{
        "id": 3,
        "user_id": 22,
        "coin": "btc",
        "coin_quantity": 4.24524129,
        "order": 1,
        "order_price": 175,
        "total_amount": 742.92,
        "order_type": 0,
        "processed": 3.85931026,
        "remaining": 0.02425852,
        "status": 1,
        "t_fee_inr": 125.92,
        "t_fee_coin": 0,
        "t_gst": 22.66,
        "invoice": null,
        "create_time": "2018-03-20T21:22:49.000Z",
        "complete_time": null
    }, {
        "id": 5,
        "user_id": 22,
        "coin": "btc",
        "coin_quantity": 2.24524129,
        "order": 1,
        "order_price": 174.8,
        "total_amount": 392.47,
        "order_type": 0,
        "processed": 0,
        "remaining": 2.24524129,
        "status": 0,
        "t_fee_inr": 0,
        "t_fee_coin": 0,
        "t_gst": 0,
        "invoice": null,
        "create_time": "2018-03-21T19:41:19.000Z",
        "complete_time": null
    }, {
        "id": 7,
        "user_id": 22,
        "coin": "btc",
        "coin_quantity": 0.64524129,
        "order": 1,
        "order_price": 174.85,
        "total_amount": 112.82,
        "order_type": 0,
        "processed": 0,
        "remaining": 0.64524129,
        "status": 0,
        "t_fee_inr": 0,
        "t_fee_coin": 0,
        "t_gst": 0,
        "invoice": null,
        "create_time": "2018-03-21T19:42:08.000Z",
        "complete_time": null
    }, {
        "id": 9,
        "user_id": 22,
        "coin": "btc",
        "coin_quantity": 0.76324129,
        "order": 1,
        "order_price": 174.89,
        "total_amount": 133.48,
        "order_type": 0,
        "processed": 0,
        "remaining": 0.76324129,
        "status": 0,
        "t_fee_inr": 0,
        "t_fee_coin": 0,
        "t_gst": 0,
        "invoice": null,
        "create_time": "2018-03-21T19:43:07.000Z",
        "complete_time": null
    }],
    [{
        "total_buy_orders": 4
    }], {
        "page_no": "1"
    }
]

【问题讨论】:

  • @TimCastelijns 不,先生,我的问题不是解析 json 数据,我知道我的问题是针对“FOR LOOP”,因为内部数组的索引值会动态变化,否则我知道如何解析这个通过将上述情况的索引值设置为 0 来获取 android 中的数据。
  • 我认为不解析是不可能的,至少你必须将它解析成json数组,然后应该可以迭代json数组对象。如果是动态数据,您可以尝试查看是否有可能检查 json 数组上的 contains 之类的内容(也许Arrays.asList(yourArray).contains(yourValue) 我猜这很有用)只是您不能迭代 String 或某些完整单元的含义,每个 json 对象
  • 数组中的索引值是无关紧要的,因为你循环的是对象,而不是索引。您的问题完全是关于 json 解析的,您只是没有看到它

标签: java arrays json for-loop


【解决方案1】:

你可以试试这个方法

JSONArray array = yourJsonArrayFromServer;

for(int i = 0 ; i < array.length(); i++){
    // get current object with: array.getJSONObject(i)
    // do something
}

如果我理解你的问题。

【讨论】:

    【解决方案2】:
    var test = []; //your array  
      for(let key of test){
            if(key instanceof Array) {
                console.log('array'); //detect if its an array then do iteration
                getValues(key);
            }else{
                console.log(key); //value to access, its an object
            }
        }
    
        function getValues(key){
            for(let val of key){
                console.log(val); //value to access, its an object
            }
        }
    

    【讨论】:

      【解决方案3】:

      这将适用于您使用 JQuery .each() 函数

      $.each(json, function (key, data) {
        $.each(data, function (index, data) {
          console.log('index', data)
        }) 
      });
      

      json 是你的 JsonArray 数据

      【讨论】:

      • 嗨,我猜他想用java解决它,他只是从节点获取数据:)
      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2014-02-15
      • 1970-01-01
      • 2023-01-10
      • 2021-09-12
      相关资源
      最近更新 更多