简体   繁体   English

如何使用 Json.simple 解析 Java 中的 JSONArray?

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

I am trying to read a JSON file like this:我正在尝试读取这样的 JSON 文件:

{
  "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.我选择尝试使用 JSON.simple(来自 GoogleCode),但我对另一个 JSON 库持开放态度。 I heard about Jackson and Gson: Are they better than JSON.simple?我听说过 Jackson 和 Gson:他们比 JSON.simple 更好吗?

Here is my current Java code:这是我当前的 Java 代码:

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.我查看了很多示例(一些在 Stack 上!)但我从未找到解决我的问题的方法。

Maybe we can't do this with JSON.simple?也许我们不能用 JSON.simple 做到这一点? What do you recommend?您有什么推荐的吗?

You never assign a new value to jsonObject , so inside the loop it still refers to the full data object.您永远不会为jsonObject分配新值,因此在循环内它仍然引用完整的数据对象。 I think you want something like:我想你想要这样的东西:

JSONObject slide = i.next();
String title = (String)slide.get("title");

It's working!它正在工作! Thx Russell.谢谢罗素。 I will finish my exercice and try GSON to see the difference.我将完成我的练习并尝试 GSON 以查看差异。

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);
        }

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,您可以在此处粘贴您的 json 文件: https : //www.freecodeformat.com/json2pojo.php创建适当的 pojo 类,然后使用以下代码:

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM