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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18831948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:28:26  来源:igfitidea点击:

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

javajsonarrays

提问by Jibi

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. I heard about Hymanson and Gson: Are they better than JSON.simple?

这是一个学校练习。我选择尝试使用 JSON.simple(来自 GoogleCode),但我对另一个 JSON 库持开放态度。我听说过 Hymanson 和 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? What do you recommend?

也许我们不能用 JSON.simple 做到这一点?你有什么建议吗?

采纳答案by Russell Zahniser

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,因此在循环内它仍然引用完整的数据对象。我想你想要这样的东西:

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

回答by Jibi

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