0

I need to read an arraylist with JSON simple in java. I got the code but what should I write to make it read the tree "example-array" that is a sub of the main tree "Example".

Here's the json code:

{
  "Example": {
    "example-array": [
      "something"
}

What I tried is reading it like

JSONArray example = (JSONArray) jsonObject.get("Example.example-array");

But that doesn't work. Help please.

Addemod C
  • 23
  • 6

1 Answers1

1

First thing: Your JSON is malformed, you didn't close the array with a "]".

But besides that, try this:

JSONParser parser=new JSONParser();

System.out.println("=======decode=======");

String s="{\"Example\":{\"example-array\":[\"something\"]}}";

Object obj=parser.parse(s);
JSONObject jObj=(JSONObject)obj;
JSONObject jObj2=(JSONObject)jObj.get("Example");
JSONArray jArr = (JSONArray)jObj2.get("example-array");

System.out.println(jArr);
System.out.println(jArr.get(0));
Usmann
  • 31
  • 4
  • Thanks! This will do the trick. And yes, I see now that the JSOn in malformed but I wrote it on my phone and it's not even a real code. I just did an example! I would upvote you but I don't have enough points myself to do so! – Addemod C Mar 25 '15 at 13:54