1

I'm using org.json to lookup json objects and values (org.json is a requirement), and I'm trying to reach the child array elements.

My json:

{
   "Info": {
     "name": "my_json",
},
   "my_array": {
     "arrays": [
       {
       "array 1": [
         {
           "name": "red",
           "server": "red1",
           "capacity": "123"
         },
        {
           "name": "blue",
           "server": "blue1",
           "capacity": "456"
        }
     ]
   },
   {
      "array 2": [
        {
          "name": "white",
          "server": "white1",
          "capacity": "1234"
        },
        {
          "name": "black",
          "server": "black1",
          "capacity": "4567"
        }
      ]
    }
  ]
 }
}

This outputs:

{"array 1":[
     {"name":"red","capacity":"123","server":"red1"},
     {"capacity":"456","name":"blue","name":"blue1"}
]}
{"array 2":[
     {"capacacity":"1234","name":"white","server":"white1"},
     {"name":"black","capacity":"4567","server":"black1"}
]}
{"array 1":[
     {"name":"red","capacity":"123","server":"red1"},
     {"capacity":"456","name":"blue","name":"blue1"}
]}
{"array 2":[
     {"capacity":"1234","name":"white","server":"white1"},
     {"name":"black","capacity":"4567","server":"black1"}
]}

The method looks like:

 public static String processJson(String[] args) throws JSONException {
    String value = "";
    String jsonData = readFile(args[0]);
    JSONObject jobj = new JSONObject(jsonData);
    if (args[1].equals("my_array")) {
        JSONObject parent = jobj.getJSONObject("my_array");
        JSONArray jarr = parent.getJSONArray("arrays");
        for (int i = 0; i < jarr.length(); i++) {
            for (int j = 0; j < jarr.length(); j++) {
                JSONObject test1 = jarr.getJSONObject(j);
                System.out.println(test1);
            }
        }
    }
    return value;
}

I would like the return value to be:

[{"name":"red","capacity":"123","server":"red1"
{"capacity":"456","name":"blue","name":"blue1"}]

Is it possible to get array 1 elements? I thought the nested loop will take care of it, but it only outputs the same time.

3
  • What is args[1] ? Commented Aug 2, 2017 at 7:26
  • @jeanr I guess it is my_array Commented Aug 2, 2017 at 7:31
  • When I try to read the structure of your json payload it looks as this:my_array.arrays[0]."array 1" my_array.arrays[1]."array 1" Are you sure your json format is arranged neatly i.e. in a proper hierarchy or relationship? Commented Aug 2, 2017 at 7:43

1 Answer 1

2

If you only want the first element then you don't need a loop

       JSONObject test1 = jarr.getJSONObject(0);
       System.out.println(test1);

If you want to format test1 your can

System.out.println (test1.toString ().replace ("{\"array 1\":", ""));
Sign up to request clarification or add additional context in comments.

3 Comments

Nice! Removed both loops!
How would you remove array 1 if it was passed as an argument? Foe example, replace("{\"variable name\":", "") doesn't remove the string.
You can use variables like str.replace (var, "") where var is a String like var = "{\"" + "array 1" + "\":";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.