0

Hi i am not able to do looping for the below multidimensional JsonArray Data which is coming from nodejs Server.

Even the index values of the jsonarray will also increase dynamically whenever more data will come into it. Please help me to implement for loop to get data dynamically.

Response which is i am getting from server is:-

[
    [{
        "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"
    }
]
  • 1
    https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Tim Mar 23 '18 at 10:30
  • 1
    @TimCastelijns no sir my question is not parse the json data, that thing i know my question is for "FOR LOOP" as the index value for inside array will change dynamically otherwise i know how to parse this data in android by putting the index value 0 for the above case. – Himanshu Agnihotri Mar 23 '18 at 10:32
  • I think its not possible without parsing, at least you will have to parse it into json array, then it should be possible to iterate over json array object. In case of dynamic data you can try to looks if there is some possibility to check something like contains on json arrays (maybe `Arrays.asList(yourArray).contains(yourValue)` I guess can be useful) simply you cannot to iterate over String or whatever in meaning of some complete units, each json objects – xxxvodnikxxx Mar 23 '18 at 11:04
  • the index value inside array is irrelevant since you're looping over objects, not indices. Your question is totally about json parsing, you just don't see it – Tim Mar 23 '18 at 12:00

3 Answers3

1

You can try this way

JSONArray array = yourJsonArrayFromServer;

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

If I understand well your question.

0
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
        }
    }
0

This will work for you using JQuery .each() function

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

json is your JsonArray Data