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

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

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

问题

  1. {
  2. "ab": {
  3. "cd": {
  4. "e": "foo",
  5. "f": "bar"
  6. },
  7. "g": "foo2"
  8. }
  9. }
英文:

I want to convert this JSON:

  1. {
  2. "ab.g": "foo2",
  3. "ab.cd.f": "bar",
  4. "ab.cd.e": "foo"
  5. }

to this:

  1. {
  2. "ab": {
  3. "cd": {
  4. "e": "foo",
  5. "f": "bar"
  6. },
  7. "g": "foo2"
  8. }
  9. }

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

以下是翻译好的部分:

回答自己的问题:

  1. import io.vertx.core.json.JsonObject;
  2. public class Tester {
  3. public static void main(String[] args) {
  4. JsonObject jsonObject = new JsonObject();
  5. deepenJsonWithDotNotation(jsonObject, "ab.cd.e", "foo");
  6. deepenJsonWithDotNotation(jsonObject, "ab.cd.f", "bar");
  7. deepenJsonWithDotNotation(jsonObject, "ab.g", "foo2");
  8. }
  9. private static void deepenJsonWithDotNotation(JsonObject jsonObject, String key, String value) {
  10. if (key.contains(".")) {
  11. String innerKey = key.substring(0, key.indexOf("."));
  12. String remaining = key.substring(key.indexOf(".") + 1);
  13. if (jsonObject.containsKey(innerKey)) {
  14. deepenJsonWithDotNotation(jsonObject.getJsonObject(innerKey), remaining, value);
  15. } else {
  16. JsonObject innerJson = new JsonObject();
  17. jsonObject.put(innerKey, innerJson);
  18. deepenJsonWithDotNotation(innerJson, remaining, value);
  19. }
  20. } else {
  21. jsonObject.put(key, value);
  22. }
  23. }
  24. }
英文:

Answering my own question:

  1. import io.vertx.core.json.JsonObject;
  2. public class Tester {
  3. public static void main(String[] args) {
  4. JsonObject jsonObject = new JsonObject();
  5. deepenJsonWithDotNotation(jsonObject, "ab.cd.e", "foo");
  6. deepenJsonWithDotNotation(jsonObject, "ab.cd.f", "bar");
  7. deepenJsonWithDotNotation(jsonObject, "ab.g", "foo2");
  8. }
  9. private static void deepenJsonWithDotNotation(JsonObject jsonObject, String key, String value) {
  10. if (key.contains(".")) {
  11. String innerKey = key.substring(0, key.indexOf("."));
  12. String remaining = key.substring(key.indexOf(".") + 1);
  13. if (jsonObject.containsKey(innerKey)) {
  14. deepenJsonWithDotNotation(jsonObject.getJsonObject(innerKey), remaining, value);
  15. } else {
  16. JsonObject innerJson = new JsonObject();
  17. jsonObject.put(innerKey, innerJson);
  18. deepenJsonWithDotNotation(innerJson, remaining, value);
  19. }
  20. } else {
  21. jsonObject.put(key, value);
  22. }
  23. }
  24. }

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:

确定