0

使用 org.json.simple 库,我如何遍历 JSONArray,处理其单个对象和单个对象内的数组?

我已经找到了如何为不同的 JSON 库执行此操作的答案,但在我的情况下,我需要使用 org.json.simple。

这是我需要处理的 JSONArray 的示例:

[
   {
      "versions":[
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.0.1.0",
            "uploaded":1429350563,
            "changelog":"<ul>\r\n\t<li>ADDED: anti-suicide.imune permission<\/li>\r\n<\/ul>",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/AntiSuicide3.zip"
         },
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.0.0.2",
            "uploaded":1429283276,
            "changelog":"<ul>\r\n\t<li>FIXED: hopefully fixed the allocated memory (needs testing)<\/li>\r\n<\/ul>",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/AntiSuicide2.zip"
         },
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.0.0.1",
            "uploaded":1429196020,
            "changelog":"<ul>\r\n\t<li>FIXED: small exception every time someone suicide.<\/li>\r\n<\/ul>",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/AntiSuicide1.zip"
         },
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.0.0.0",
            "uploaded":1428923374,
            "changelog":"",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/AntiSuicide.zip"
         }
      ],
      "author":"ApokPT",
      "name":"Anti-Suicide",
      "id":910,
      "development_stage":"Release",
      "headline":"Anti-Suicide relocation",
      "url":"https:\/\/dev.rocket.foundation\/?post_type=rplugin&#038;p=910"
   },
   {
      "versions":[
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.0.0.0",
            "uploaded":1429113571,
            "changelog":"Updated needed libraries",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/ZaupWhitelist_1.0.0.02.zip"
         },
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.0.0.0",
            "uploaded":1429047352,
            "changelog":"First release",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/ZaupWhitelist_1.0.0.01.zip"
         }
      ],
      "author":"Zamirathe",
      "name":"Zaup Mysql Whitelist",
      "id":929,
      "development_stage":"Release",
      "headline":"Whitelist in Mysql",
      "url":"https:\/\/dev.rocket.foundation\/?post_type=rplugin&#038;p=929"
   },
   {
      "versions":[
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.3.5.0",
            "uploaded":1429284290,
            "changelog":"<ul>\r\n\t<li>FIXED: Possible memory leak (needs testing);<\/li>\r\n\t<li>FIXED: Reactivated strip on give kit;<\/li>\r\n\t<li>ADDED: Cooldown check for permission givekit.onjoin.&lt;kitname&gt;<\/li>\r\n<\/ul>",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/GiveKit27.zip"
         },
         {
            "rocket_version":"3.3.0.0",
            "pluginVersion":"1.3.4.0",
            "uploaded":1429192200,
            "changelog":"<ul>\r\n\t<li>ADDED: givekit.onjoin.&lt;kit name&gt; permission<\/li>\r\n<\/ul>",
            "url":"https:\/\/dev.rocket.foundation\/wp-content\/uploads\/GiveKit26.zip"
         }
      ],
      "author":"ApokPT",
      "name":"Give Kit",
      "id":858,
      "development_stage":"Release",
      "headline":"Give a Kit to another player or yourself",
      "url":"https:\/\/dev.rocket.foundation\/?post_type=rplugin&#038;p=858"
   }
]

整个数组中的每个单独对象都有一个版本数组和一个“id”属性。对于每个单独的对象,我需要:

  1. 获取 'id' 属性的值
  2. 遍历 'versions' 数组中的每个对象,并获取其属性的值,例如 'url'、'changelog'、'pluginVersion' 等。

对于每个版本数组中的每个对象,我只需要调用一个接受 id 属性和 url/changelog/pluginVersion 属性的方法。

如何使用 org.json.simple 库在 Java 中完成此任务?

注意:我需要使用 org.json.simple 库!

4

1 回答 1

0

从 README 文件看来,解析对象只是为您提供了一些扩展基本 java 集合类的类(org.json.simple.JSONObject 继承 java.util.HashMap 和 org.json.simple.JSONArray 继承 java.util。 ArrayList),因此您可以使用所有普通的 java 方法。例如:

  String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";

  Object obj=JSONValue.parse(s);
  JSONArray array=(JSONArray)obj;
  System.out.println(array.get(1));

  JSONObject obj2=(JSONObject)array.get(1);
  System.out.println(obj2.get("1"));

  Result:
  {"1":{"2":{"3":{"4":[5,{"6":7}]}}}}
  {"2":{"3":{"4":[5,{"6":7}]}}}
于 2015-04-20T15:56:55.490 回答