提问者:小点点

Java:HashMap嵌套JSON对象


努力找出如何创建基于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中的键/值对可以是任意数量的项目,而不是像我在上面的示例中描述的那样只有两个。我尝试过做递归函数,但我一直碰壁。

编辑:这不是这个问题的重复。我知道如何使用这些方法将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个答案

匿名用户

解决了这个问题:

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