Home > database >  How to iterate through an array in an array?
How to iterate through an array in an array?

Time:12-19

I need to parse a JSON string in Java. I am using JSONObject to parse the string and get the object. I don't know how to loop through a triple array without knowing the keys.

This is the JSON as string:

{ "version": "0.8.0", "generator": "vzlogger", "data": [ { "uuid": "d495a390-f747-11e0-b3ca-f7890e45c7b2", "last": 0, "interval": -1, "protocol": "s0" }, { "uuid": "a76ffbb0-5fcb-11ec-afdd-597654871263", "last": 1639902960610, "interval": 0, "protocol": "d0", "tuples": [ [ 1639902960610, 33067 ] ] } ] 

I need to loop through each data and get for each entry the uuid. And I need to get for each uuid the tuples. For example

uuid a76ffbb0-5fcb-11ec-afdd-597654871263 
first tuples 1639902960610 
second tuples 33067
...

In the array are 50 uuids, in the example above I have only copied the first.

This is my code:

JSONObject obj = http.getResponseJSON();
            
JSONArray arr = obj.getJSONArray("data"); // notice that `"posts": [...]`

for (int i = 0; i < arr.length(); i  ){
    String uuid = arr.getJSONObject(i).getString("uuid");
    if (arr.getJSONObject(i).has("tuples")) {
        JSONArray tuples = arr.getJSONObject(i).getJSONArray("tuples");
        log.println("UUID: " uuid   "CNT: " tuples.length());
        for (int j = 0; j < arr.length(); j  ){
            String tuple = tuples.getJSONObject(j).get ... HELP ... THERE IS NO KEY ....    
        }
    }
}

CodePudding user response:

You have nested array in tuples field. so first

  1. traverse the outer array
  2. then for every index, traverse the inner array and print the value directly.

Here is the working code

            if (arr.getJSONObject(i).has("tuples")) {
                JSONArray outerTuples = arr.getJSONObject(i).getJSONArray("tuples");
                System.out.println("UUID: "   uuid);
                for (int k = 0; k < outerTuples.length(); k  ) {
                    JSONArray innerTuples = outerTuples.getJSONArray(k);
                    for (int j = 0; j < innerTuples.length(); j  ) {
                        System.out.println(innerTuples.get(j));
                    }
                }
            }

CodePudding user response:

Method 1: Using for loop: This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.

import java.io.*; class GFG {

public static void main(String args[]) throws IOException
{
    int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int i, x;

    // iterating over an array
    for (i = 0; i < ar.length; i  ) {

        // accessing each element of array
        x = ar[i];
        System.out.print(x   " ");
    }
}

}

method 2 Using for each loop : For each loop optimizes the code, save typing and time.

import java.io.*; class GFG {

public static void main(String args[]) throws IOException
{
    int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int x;

    // iterating over an array
    for (int i : ar) {

        // accessing each element of array
        x = i;
        System.out.print(x   " ");
    }
}

}

  • Related