从jackson中删除objectnode中的所有元素

时间:2018-04-26 11:21:11

标签: java json jackson-databind

我有以下objectNode:

{
   "type": "FeatureCollection",
   "features": [
        {"type":"Feature",
         "properties":{
              "pro1":"value1",
              "pro2":"value2" 
         },
         "geometry":{
              "type":"Polygon",
              "coordinates":[[[3.22998,48.312428],[3.22998,48.719961],[3.405762,48.719961],[3.405762,48.312428],[3.22998,48.312428]]]
           }
         }  
      ]
}

基本上我想从属性中删除所有元素并获得类似的内容:

"properties":{}

我尝试过使用以下内容,但它会删除所有内容:

bufferPolygon.setObject(((ObjectNode)bufferIteration).with("properties").removeAll().toString());

1 个答案:

答案 0 :(得分:0)

  

基本上我想删除properties中的所有元素并得到类似的内容:

"properties":{}

以下内容可为您提供所需的结果:

JsonNode tree = mapper.readTree(json);
for (JsonNode feature : tree.get("features")) {
    ((ObjectNode) feature).set("properties", mapper.createObjectNode());
}

String updatedJson = mapper.writeValueAsString(tree);