简体   繁体   English

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

[英]Java: HashMap Into Nested JSON Object

Struggling to figure out how to create nested JSON based of a HashMap. 努力弄清楚如何基于HashMap创建嵌套的JSON。 Basically I have a List of HashMap, that has non unique key/value pairs. 基本上我有一个HashMap列表,它具有非唯一的键/值对。 And I need to turn this List of HashMap into a unique JSON Object. 而且我需要将此HashMap列表转换为唯一的JSON对象。

Here is an example of what I'm trying to describe: List of HashMap looks like: 这是我要描述的示例: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}, 
]

And I'd like my result to be: 我希望我的结果是:

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

One caveat is that they key/value pair in the HashMap can be any number of items, not just two like I've described in the example above. 一个警告是,它们在HashMap中的键/值对可以是任意数量的项,而不仅仅是上面示例中描述的两项。 I've tried doing a recursive function, but I keep hitting a wall. 我试图做一个递归函数,但是我一直碰壁。

Edit: This is not a duplicate of this question . 编辑:这不是此问题的重复。 I know how to serialize a HashMap into a JsonObject using these methods. 我知道如何使用这些方法将HashMap序列化为JsonObject。 I've done it several times. 我已经做过几次了。 I'm asking how to logically analyze data that is in a HashMap and create a JsonObject based of that logic. 我在问如何逻辑上分析HashMap中的数据并基于该逻辑创建JsonObject。

Edit 2: Example of multiple levels: 编辑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"}, 
]

And here is the JSON: 这是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"
                    ]
                }
        }
    ] 
}

Solved this issue: 解决了这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM