Java:将HashMap转换为嵌套JSON对象

时间:2018-12-10 14:19:31

标签: java json recursion hashmap

努力弄清楚如何基于HashMap创建嵌套的JSON。基本上我有一个HashMap列表,它具有非唯一的键/值对。而且我需要将此HashMap列表转换为唯一的JSON对象。

以下是我要描述的示例: HashMap的列表如下:

[
    {PERIOD: 201801, ID: 12345},
    {PERIOD: 201801, ID: 12346},
    {PERIOD: 201801, ID: 12347},
    {PERIOD: 201802, ID: 12345},
    {PERIOD: 201802, ID: 12347},
    {PERIOD: 201803, ID: 12345}, 
]

我希望我的结果是:

{
    "NAME": "Results",
    "DETAILS": [
        {
            "PERIOD": 201801,
            "DETAILS": [
                { "ID": 12345 },
                { "ID": 12346 },
                { "ID": 12347 }
        },
        {
            "PERIOD": 201802,
            "DETAILS": [
                { "ID": 12345 },
                { "ID": 12347 }
        },
        {
            "PERIOD": 201803,
            "DETAILS": [
                { "ID": 12345 }
        }
    ] 
}

一个警告是,它们在HashMap中的键/值对可以是任意数量的项,而不仅仅是上面示例中描述的两项。我曾经尝试做一个递归函数,但是我一直碰壁。

编辑:这不是this question的重复项。我知道如何使用这些方法将HashMap序列化为JsonObject。我已经做过几次了。我在问如何逻辑分析HashMap中的数据并基于该逻辑创建JsonObject。

编辑2:多个级别的示例:

[
  {PERIOD: 201801, ID: 12345, MANAGER: "Dave"},
  {PERIOD: 201801, ID: 12345, MANAGER: "Jill"},
  {PERIOD: 201801, ID: 12346, MANAGER: "Dave"},
  {PERIOD: 201801, ID: 12347, MANAGER: "Jon"},
  {PERIOD: 201802, ID: 12345, MANAGER: "Rob"},
  {PERIOD: 201802, ID: 12347, MANAGER: "Dave"},
  {PERIOD: 201803, ID: 12345, MANAGER: "Bailey"}, 
]

这是JSON:

{
"NAME": "Results",
"DETAILS": [
        {
            "PERIOD": 201801,
            "DETAILS": [
                { 
                    "ID": 12345,
                    "DETAILS": [
                       "MANAGER": "Dave",
                       "MANAGER": "Jill"
                    ]
                },
                { 
                    "ID": 12346,
                    "DETAILS": [
                        "MANAGER": "Dave"
                    ]
                },
                { 
                    "ID": 12347,
                    "DETAILS": [
                        "MANAGER": "Jon"
                    ] 
                }
        },
        {
            "PERIOD": 201802,
            "DETAILS": [
                { 
                    "ID": 12345,
                    "DETAILS": [
                        "MANAGER": "Rob"
                    ] 
                },
                { 
                    "ID": 12347,
                    "DETAILS": [
                        "MANAGER": "Dave"
                    ]
                }
        },
        {
            "PERIOD": 201803,
            "DETAILS": [
                { 
                    "ID": 12345,
                    "DETAILS": [
                        "MANAGER": "Bailey"
                    ]
                }
        }
    ] 
}

1 个答案:

答案 0 :(得分:0)

解决了这个问题:

private void insertValueIntoDetails(String key, String value, JsonArray details,  int targetIndex, int currentIndex) {
    // if the targetIndex equals the currentIndex ?
    if (targetIndex == currentIndex) {
        // if details doesn't have the value, then insert it
        AtomicBoolean hasValue = new AtomicBoolean(false);
        details.forEach(item -> {
            if (((JsonObject)item).get("ID").getAsString().equals(value.toString())) {
                hasValue.set(true);
            }
        });
        if (!hasValue.get()) {
            JsonObject o = new JsonObject();
            o.addProperty("ID", value);
            o.add("Details", new JsonArray());
            details.add(o);
        }
    }
    // if the targetIndex > currentIndex
    if (targetIndex > currentIndex) {
        // call insertValueIntoDetails with key, value, details of the currentIndex, targetIndex, currentIndex + 1
        JsonArray deets = getDetails(key, details, currentIndex);
        insertValueIntoDetails(key, value, deets, targetIndex, currentIndex + 1);
    }
}