4

Looking for org-json solutions only please **

Suppose you deal with a structure, as follows.

Using json-org, how can i get an array of Tests, if this entire json is represented in a String s?

Using Google's gson, it's easy to do by testing what type given object is ... i am missing something simple here with json-org libraries

{
   "Groups":{
      "Test":[
         {
            "Test Number":123456,
            "Channel":"TEST",
            "environment":"A",
            "output event":[
               {
                  "description":"very good description",
                  "value":123,
                  "active":true
               },
               {
                  "description":"another very good description",
                  "value":456,
                  "active":true
               }
            ],
            "active":true,
            "instrument":"ABC"
         },
         {
            "Test Number":547985,
            "Channel":"some new channel",
            "environment":"B",
            "output event":[
               {
                  "description":"reject",
                  "value":123,
                  "active":true
               },
               {
                  "description":"ack",
                  "value":456,
                  "active":true
               }
            ],
            "active":true,
            "instrument":"XYZ"
         }
      ],
      "name":"A clever name",
      "active":true
   }
}

1 Answer 1

8

It so happens i got it before someone else had a chance to help. In case someone else has similar question, i am posting an answer below:

JSONObject o = new JSONObject(s);

JSONArray arrayOfTests = (JSONArray) ((JSONObject) o.get("Groups")).get("Test");

for (int i = 0; i < arrayOfTests.length(); i++) {
    System.out.println(arrayOfTests.get(i));
}
Sign up to request clarification or add additional context in comments.

2 Comments

JSONObject o = new JSONObject(s); In this line i got eroor. "The constructor JSONObject is undefined".
What is "s" in your case? Should be String object, i believe

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.