简体   繁体   English

如何使用 Jackson 将 ObjectNode 添加到子 ObjectNode 作为父级

[英]How to Add ObjectNode to child ObjectNode as Parent using Jackson

I want to add 2 Nodes inside of a nested structure.我想在嵌套结构中添加 2 个节点。 The first one is the initial state that I have.第一个是我拥有的初始 state。

INITIAL 

{
  "type" : "type",
  "features" : [ {
    "properties" : {
      "staticText" : "1",
      "assets" : {
        "metaData" : {
          "someMetaData"
        }
      }
    }
  } ],
}

I want to add "merge" and "node" object nodes like below.我想添加“合并”和“节点”object 节点,如下所示。 Then I will be having the below one.然后我将有下面的。

DESIRED

{
  "type" : "type",
  "features" : [ {
    "merge" : {
     "properties" : {
       "staticText" : "1",
       "assets" : {
         "node": {
          "metaData" : {
            "someMetaData"
          }
         }
       }
     } 
   }
  } ],
}

I tried to put empty Nodes("jsonNodes") at the below code but did not work out.我试图将空节点(“jsonNodes”)放在下面的代码中,但没有成功。 How can I state at 2nd nested Nodes?如何在第二个嵌套节点上实现 state?

root.with("merge").set("properties",jsonNodes);

https://github.com/octomix/josson https://github.com/octomix/josson

Deserialization反序列化

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"type\" : \"type\"," +
    "  \"features\" : [ {" +
    "    \"properties\" : {" +
    "      \"staticText\" : \"1\"," +
    "      \"assets\" : {" +
    "        \"metaData\" : {" +
    "          \"someMetaData\" : \"xxx\"" +
    "        }" +
    "      }" +
    "    }" +
    "  } ]" +
    "}");

Transformation转型

JsonNode node = josson.getNode(
    "field(features" +
    "  .map(merge" +
    "    :field(properties" +
    "      .field(assets" +
    "        .map(node" +
    "          :field(metaData)" +
    "        )" +
    "      )" +
    "    )" +
    "  )" +
    ")");
System.out.println(node.toPrettyString());

Output Output

{
  "type" : "type",
  "features" : [ {
    "merge" : {
      "properties" : {
        "staticText" : "1",
        "assets" : {
          "node" : {
            "metaData" : {
              "someMetaData" : "xxx"
            }
          }
        }
      }
    }
  } ]
}

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

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