Marklogic - 使用java api

时间:2016-06-22 12:08:09

标签: json jackson marklogic marklogic-8

让我们说json看起来像

{ 
   "name":"abc", 
   "lastName":"xyz", 
   "description":"aaaaa aaaa", 
   "dob":11-10-1988,
   "workInformation":[
       {
          "address":"kolkata", 
          "workFor":"vvv Pvt Ltd",
          "reference" : [
              {
                 "refName" : "ttt",
                 "refId" : "12345"
              },
               {
                 "refName" : "sss",
                 "refId" : "23412"
              }
          ]
       },
        {
          "address":"bangalore", 
          "workFor":"www Pvt Ltd",
          "reference" : [
              {
                 "refName" : "rrr",
                 "refId" : "43434"
              },
               {
                 "refName" : "yyyy",
                 "refId" : "34213"
              }
          ]
       },
       {
          "address":"delhi", 
          "workFor":"sss Pvt Ltd",
          "reference" : [
              {
                 "refName" : "qqqq",
                 "refId" : "76767"
              },
               {
                 "refName" : "gggg",
                 "refId" : "65432"
              }
          ]
       }
    ]
}

我尝试过DocumentPatchBuilder的insertFragment。使用此我可以在json属性之前/之后更新。但我必须在属性workInformation中插入数组类型。这是我尝试的insertFragment示例 -

 DocumentPatchBuilder pb = docMgr.newPatchBuilder();
 pb.pathLanguage(DocumentPatchBuilder.PathLanguage.JSONPATH);
 ObjectMapper mapper = new ObjectMapper();
 pb.insertFragment("workInformation", Position.BEFORE,mapper.createObjectNode().put("hello", "hai"));

我想使用java api在 workInformation 部分中插入下面提到的数据 -

   {
          "address":"Mumbai", 
          "workFor":"zzz Pvt Ltd"
   }

请让我知道怎么做。

感谢阅读。

1 个答案:

答案 0 :(得分:5)

要在workInformation数组中插入,您需要将位置指定为POSITION.LAST_CHILD,以将其作为上下文子列表的最后一个子项插入。此外,您需要在patchBuilder.insertFragment的第一个参数中指定“workInformation”作为数组类型 - [“workInformation”]。代码类似于:

DocumentPatchBuilder pb = docMgr.newPatchBuilder();
pb.pathLanguage(DocumentPatchBuilder.PathLanguage.JSONPATH);
ObjectMapper mapper = new ObjectMapper();

ObjectNode fragmentNode = mapper.createObjectNode();
fragmentNode = mapper.createObjectNode();
fragmentNode.put("address", "mumbai");
fragmentNode.put("workFor", "zzz Pvt Ltd");
String fragment = mapper.writeValueAsString(fragmentNode);

pb.insertFragment("$.[\"workInformation\"]", Position.LAST_CHILD, fragment);

这应该可以解决问题,这会插入

{
    "address":"Mumbai", 
    "workFor":"zzz Pvt Ltd"
}

在“workInformation”属性的末尾。