0

Lets say I want to parse a JSONObject with JSONObjects inside of it, which I parse from a string. I want to do this in a single line, like I've done with other libraries, but I'm not sure how.

   JSONParser parser = new JSONParser();
   Object obj = parser.parse(test);
   JSONObject first = (JSONObject) obj;
   JSONObject second = (JSONObject) first.get("feed");
   JSONArray third = (JSONArray) second.get("entry");
   JSONObject fourth = (JSONObject) third.get(0);
   JSONObject fifth = (JSONObject) fourth.get("test");

Is there a way for me to get all these JSONObjects in a single line? With another library I'd just do first.getJSONObject("feed").getJSONArray("entry").getJSONObject(0) etc, but I'm not sure how to do it properly with this library.

Thanks.

mathExpl
  • 79
  • 1
  • 1
  • 5
  • `With another library I'd just do first.getJSONObject("feed").getJSONArray("entry").getJSONObject(0)` -- What sort of error do you get when you try that with this library? – azurefrog Mar 13 '17 at 22:13
  • The method getJSONObject(String) is undefined for the type JSONObject – mathExpl Mar 13 '17 at 22:19

2 Answers2

0

I recommend use Gson (https://github.com/google/gson) Library from Google.

ELZIPA
  • 26
  • 2
-1

You can cast within a single line like this:

(JSONObject) ((JSONObject) YOURJSONOBJECT.get("YOUR_KEY")).get("ANOTHER_KEY");

This can get messy really quick depending on how many layers deep you have to go though

Naberhausj
  • 303
  • 1
  • 9