【问题标题】:How do I parse a JSONArray in Java with Json.simple?如何使用 Json.simple 解析 Java 中的 JSONArray?
【发布时间】:2013-09-20 20:02:23
【问题描述】:

我正在尝试读取这样的 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."
        }
      ]
    }
  ]
}

这是为了学校练习。我选择尝试使用 JSON.simple(来自 GoogleCode),但我对另一个 JSON 库持开放态度。我听说过 Jackson 和 Gson:它们比 JSON.simple 更好吗?

这是我当前的 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);
}

我查看了很多示例(一些在 Stack 上!),但我从未找到解决问题的方法。

也许我们不能用 JSON.simple 做到这一点?你有什么推荐的?

【问题讨论】:

  • 这对我来说纯粹是一个宗教论点,但我更喜欢 Google GSON 而不是其他 JSON 解析器。

标签: java json arrays


【解决方案1】:

您永远不会为jsonObject 分配新值,因此在循环内它仍然引用完整的数据对象。我想你想要这样的东西:

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

【讨论】:

    【解决方案2】:

    它正在工作!谢谢罗素。我会完成我的练习并尝试使用 GSON 来看看有什么不同。

    这里有新代码:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-03
        相关资源
        最近更新 更多