如何将 Java 中的点符号表示的 JsonObject 转换为嵌套的 JsonObject?

huangapple go评论65阅读模式
英文:

How to convert dot notation JsonObject to nested JsonObject in Java?

问题

{
   "ab": {
      "cd": {
         "e": "foo",
         "f": "bar"
      },
      "g": "foo2"
   }
}
英文:

I want to convert this JSON:

{
    "ab.g": "foo2",
    "ab.cd.f": "bar",
    "ab.cd.e": "foo"
}

to this:

{
   "ab": {
      "cd": {
         "e": "foo",
         "f": "bar"
      },
      "g": "foo2"
   }
}

I have found this: Convert javascript dot notation object to nested object but the accepted answer on this post has some language specific syntax and I am not able to rewrite the same logic in Java.

Note: There is no attempt in the question because I have answered this myself - because there is no such question on stackoverflow for Java specifically.

答案1

得分: 3

以下是翻译好的部分:

回答自己的问题:

import io.vertx.core.json.JsonObject;

public class Tester {
    public static void main(String[] args) {
        JsonObject jsonObject = new JsonObject();
        deepenJsonWithDotNotation(jsonObject, "ab.cd.e", "foo");
        deepenJsonWithDotNotation(jsonObject, "ab.cd.f", "bar");
        deepenJsonWithDotNotation(jsonObject, "ab.g", "foo2");
    }

    private static void deepenJsonWithDotNotation(JsonObject jsonObject, String key, String value) {
        if (key.contains(".")) {
            String innerKey = key.substring(0, key.indexOf("."));
            String remaining = key.substring(key.indexOf(".") + 1);

            if (jsonObject.containsKey(innerKey)) {
                deepenJsonWithDotNotation(jsonObject.getJsonObject(innerKey), remaining, value);
            } else {
                JsonObject innerJson = new JsonObject();
                jsonObject.put(innerKey, innerJson);
                deepenJsonWithDotNotation(innerJson, remaining, value);
            }
        } else {
            jsonObject.put(key, value);
        }
    }
}
英文:

Answering my own question:

import io.vertx.core.json.JsonObject;

public class Tester {
    public static void main(String[] args) {
        JsonObject jsonObject = new JsonObject();
        deepenJsonWithDotNotation(jsonObject, "ab.cd.e", "foo");
        deepenJsonWithDotNotation(jsonObject, "ab.cd.f", "bar");
        deepenJsonWithDotNotation(jsonObject, "ab.g", "foo2");
    }

    private static void deepenJsonWithDotNotation(JsonObject jsonObject, String key, String value) {
        if (key.contains(".")) {
            String innerKey = key.substring(0, key.indexOf("."));
            String remaining = key.substring(key.indexOf(".") + 1);

            if (jsonObject.containsKey(innerKey)) {
                deepenJsonWithDotNotation(jsonObject.getJsonObject(innerKey), remaining, value);
            } else {
                JsonObject innerJson = new JsonObject();
                jsonObject.put(innerKey, innerJson);
                deepenJsonWithDotNotation(innerJson, remaining, value);
            }
        } else {
            jsonObject.put(key, value);
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月27日 07:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定