需要建议交换复杂json的键和值

时间:2018-05-14 19:26:47

标签: java json

我有以下json

{  
   "Roles":[  
      {  
         "January":[  
            {  
               "CN":[  
                  "GoogleCnFeatureOwner",
                  "GoogleCnAnalyst",
                  "SupportAnalyst",
                  "AdminCn"
               ]
            },
            {  
               "EU":[  
                  "GoogleEuFeatureOwner",
                  "GoogleEuAnalyst",
                  "SupportAnalyst",
                  "AdminEu"
               ]
            },
            {  
               "NA":[  
                  "GoogleNaFeatureOwner",
                  "GoogleNaAnalyst",
                  "SupportAnalyst",
                  "AdminNa"
               ]
            }
         ]
      },
      {  
         "February":[  
            {  
               "CN":[  
                  "AbcCnFeatureOwner",
                  "AbcCnAnalyst",
                  "SupportAnalyst",
                  "AdminCn"
               ]
            },
            {  
               "EU":[  
                  "AbcEuFeatureOwner",
                  "AbcEuAnalyst",
                  "SupportAnalyst",
                  "AdminEu"
               ]
            },
            {  
               "NA":[  
                  "AbcNaFeatureOwner",
                  "AbcNaAnalyst",
                  "SupportAnalyst",
                  "AdminNa"
               ]
            }
         ]
      },
      {  
         "March":[  
            {  
               "CN":[  
                  "YahooCnFeatureOwner",
                  "SupportAnalyst",
                  "AdminCn",
                  "UserCn"
               ]
            },
            {  
               "NA":[  
                  "YahooNaFeatureOwner",
                  "SupportAnalyst",
                  "AdminNa",
                  "UserNa"
               ]
            }
         ]
      }
   ]
}

我正在尝试在java中实现一个方法来解析上面的json并用键交换键,并创建一个像下面这样的集合json。

    {
    "WifiCnFeatureOwner": [{
        "CN": ["January"]
    }],
    "ItSupportAnalyst": [{
        "CN": ["January", "February", "March"]
    }, {
        "EU": ["January", "February"]
    }, {
        "NA": ["January", "February", "March"]
    }]
}

我正在使用org.json API开始。 请提出一些有效实现转换的最佳方法(不使用Guava BiMap)。无论如何我会在完成后发布代码。

2 个答案:

答案 0 :(得分:1)

由于您使用的是org.json API,因此这是一种方法。

我故意不做你想要的输出,所以你必须要理解修改它的代码。下面的输出具有您想要的所有功能,但它更简单。

byte[] inputBytes = Files.readAllBytes(Paths.get("test.json"));
String inputText = new String(inputBytes, StandardCharsets.US_ASCII);
JSONObject inputJson = new JSONObject(inputText);
JSONObject outputJson = new JSONObject();
for (Object monthObj : inputJson.getJSONArray("Roles")) {
    JSONObject monthJson = (JSONObject) monthObj;
    for (String monthName : monthJson.keySet()) {
        for (Object regionObj : monthJson.getJSONArray(monthName)) {
            JSONObject regionJson = (JSONObject) regionObj;
            for (String regionCode : regionJson.keySet()) {
                for (Object roleObj : regionJson.getJSONArray(regionCode)) {
                    String roleName = (String) roleObj;
                    JSONObject role = outputJson.optJSONObject(roleName);
                    if (role == null)
                        outputJson.put(roleName, role = new JSONObject());
                    JSONArray region = role.optJSONArray(regionCode);
                    if (region == null)
                        role.put(regionCode, region = new JSONArray());
                    region.put(monthName);
                }
            }
        }
    }
}
System.out.println(outputJson.toString(3));

输出

{
   "AbcNaAnalyst": {"NA": ["February"]},
   "AbcCnFeatureOwner": {"CN": ["February"]},
   "GoogleNaFeatureOwner": {"NA": ["January"]},
   "UserCn": {"CN": ["March"]},
   "AdminNa": {"NA": [
      "January",
      "February",
      "March"
   ]},
   "GoogleEuAnalyst": {"EU": ["January"]},
   "GoogleCnFeatureOwner": {"CN": ["January"]},
   "SupportAnalyst": {
      "EU": [
         "January",
         "February"
      ],
      "NA": [
         "January",
         "February",
         "March"
      ],
      "CN": [
         "January",
         "February",
         "March"
      ]
   },
   "AbcEuAnalyst": {"EU": ["February"]},
   "YahooNaFeatureOwner": {"NA": ["March"]},
   "UserNa": {"NA": ["March"]},
   "AdminCn": {"CN": [
      "January",
      "February",
      "March"
   ]},
   "AdminEu": {"EU": [
      "January",
      "February"
   ]},
   "GoogleNaAnalyst": {"NA": ["January"]},
   "YahooCnFeatureOwner": {"CN": ["March"]},
   "AbcEuFeatureOwner": {"EU": ["February"]},
   "GoogleEuFeatureOwner": {"EU": ["January"]},
   "AbcCnAnalyst": {"CN": ["February"]},
   "AbcNaFeatureOwner": {"NA": ["February"]},
   "GoogleCnAnalyst": {"CN": ["January"]}
}

为人类可读性手动重新格式化:

{
   "AbcCnAnalyst": {
      "CN": ["February"]
   },
   "AbcCnFeatureOwner": {
      "CN": ["February"]
   },
   "AbcEuAnalyst": {
      "EU": ["February"]
   },
   "AbcEuFeatureOwner": {
      "EU": ["February"]
   },
   "AbcNaAnalyst": {
      "NA": ["February"]
   },
   "AbcNaFeatureOwner": {
      "NA": ["February"]
   },
   "AdminCn": {
      "CN": ["January", "February", "March"]
   },
   "AdminEu": {
      "EU": ["January", "February"]
   },
   "AdminNa": {
      "NA": ["January", "February", "March"]
   },
   "GoogleCnAnalyst": {
      "CN": ["January"]
   },
   "GoogleCnFeatureOwner": {
      "CN": ["January"]
   },
   "GoogleEuAnalyst": {
      "EU": ["January"]
   },
   "GoogleEuFeatureOwner": {
      "EU": ["January"]
   },
   "GoogleNaAnalyst": {
      "NA": ["January"]
   },
   "GoogleNaFeatureOwner": {
      "NA": ["January"]
   },
   "SupportAnalyst": {
      "EU": ["January", "February"],
      "NA": ["January", "February", "March"],
      "CN": ["January", "February", "March"]
   },
   "UserCn": {
      "CN": ["March"]
   },
   "UserNa": {
      "NA": ["March"]
   },
   "YahooCnFeatureOwner": {
      "CN": ["March"]
   },
   "YahooNaFeatureOwner": {
      "NA": ["March"]
   }
}

答案 1 :(得分:0)

我建议不要试图重新发明轮子。 您提到的所有内容都已在开源库中完成。

  1. 使用Jackson(或其他一些JSON解析器)执行JSON的初始解析。
  2. 使用Guava BiMap获取双向地图(应该允许您交换键和值)。
  3. 使用Jackson(或其他blah)创建新的JSON。