15

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?

1
  • This is purely a religious argument for me, but I much prefer Google GSON over other JSON parsers. Commented Sep 16, 2013 at 15:47

3 Answers 3

18

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");
Sign up to request clarification or add additional context in comments.

Comments

18

It's working! Thx Russell. I will finish my exercice and try GSON to see the difference.

New code here:

        JSONArray slideContent = (JSONArray) jsonObject.get("presentationSlides");
        Iterator i = slideContent.iterator();

        while (i.hasNext()) {
            JSONObject slide = (JSONObject) i.next();
            String title = (String)slide.get("title");
            System.out.println(title);
        }

Comments

-1

For Gson you can paste your json file here : https://www.freecodeformat.com/json2pojo.php Create appropriate pojo classes and then use this code :

Gson gson = new Gson();

    try (Reader reader = new FileReader("pathToYourFile.json")) {

        // Convert JSON File to Java Object
        Root root = gson.fromJson(reader, Root.class);

        // print staff you need
        System.out.println(root.getCommands().get(0).getName());

    } catch (IOException e) {
        e.printStackTrace();
    }

Comments

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.