org.json.simple - 如何遍历JSONArray中的各个对象以及各个对象中的数组?

时间:2015-04-19 14:29:04

标签: java arrays json loops org.json

使用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"
   }
]
&#13;
&#13;
&#13;

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

  1. 获取&#39; id&#39;属性值
  2. 遍历&#39;版本中的每个对象&#39;数组并获取其属性的值,例如&#39; url&#39;,&#39; changelog&#39;,&#39; pluginVersion&#39;等。
  3. 对于每个版本数组中的每个对象,我只需要调用一个接受id属性的方法,以及url / changelog / pluginVersion属性。

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

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

1 个答案:

答案 0 :(得分:0)

从README文件中可以看出,解析对象只会为您提供一些扩展基本java集合类的类(org.json.simple.JSONObject继承java.util.HashMap&amp; 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}]}}}