Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a JSONArray in Java with Json.simple?

Tags:

java

json

arrays

I am trying to read a JSON file like this:

{
  "presentationName" : "Here some text",
  "presentationAutor" : "Here some text",
  "presentationSlides" : [
    {
      "title" : "Here some text.",
      "paragraphs" : [
        {
          "value" : "Here some text."
        },
        {
          "value" : "Here some text."
        }
      ]
    },
    {
      "title" : "Here some text.",
      "paragraphs" : [
        {
          "value" : "Here some text.",
          "image" : "Here some text."
        },
        {
          "value" : "Here some text."
        },
        {
          "value" : "Here some text."
        }
      ]
    }
  ]
}

It's for a school exercise. I chose to try and use JSON.simple (from GoogleCode), but I am open to another JSON library. I heard about Jackson and Gson: Are they better than JSON.simple?

Here is my current Java code:

Object obj = parser.parse(new FileReader( "file.json" ));

JSONObject jsonObject = (JSONObject) obj;

// First I take the global data
String name = (String) jsonObject.get("presentationName");
String autor = (String) jsonObject.get("presentationAutor");
System.out.println("Name: "+name);
System.out.println("Autor: "+autor);

// Now we try to take the data from "presentationSlides" array
JSONArray slideContent = (JSONArray) jsonObject.get("presentationSlides");
Iterator i = slideContent.iterator();

while (i.hasNext()) {
    System.out.println(i.next());
    // Here I try to take the title element from my slide but it doesn't work!
    String title = (String) jsonObject.get("title");
    System.out.println(title);
}

I checked out a lot of examples (some on Stack!) but I never found the solution to my problem.

Maybe we can't do this with JSON.simple? What do you recommend?

like image 807
Jibi Avatar asked Sep 16 '13 15:09

Jibi


People also ask

How do you parse a JSON object in Java?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

How get values from JSONArray?

String value = (String) jsonObject. get("key_name"); Just like other element retrieve the json array using the get() method into the JSONArray object.

Can JSON parse an array?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

Can we convert JSONArray to JSON object?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.


1 Answers

You never assign a new value to jsonObject, so inside the loop it still refers to the full data object. I think you want something like:

JSONObject slide = i.next();
String title = (String)slide.get("title");
like image 86
Russell Zahniser Avatar answered Oct 26 '22 00:10

Russell Zahniser