我有以下 Json{ "Parent": { "pk1": "pv1", "pk2": "pv2", "Child": { "*": { "ck1": "cv1", "ck2": "cv2" } } }}现在我想删除 "*":{} 但保留其内容。预期的输出是这样的。{ "Parent": { "pk1": "pv1", "pk2": "pv2", "Child": { "ck1": "cv1", "ck2": "cv2" } }}我如何使用 Java Jackson 来实现这一目标?
1 回答
不负相思意
TA贡献1777条经验 获得超10个赞
看下面的操作
ObjectMapper mapper = new ObjectMapper();
String file = "src/main/resources/yourjson.json";
JsonNode object = mapper.readTree(new File(file));
ObjectNode child = (ObjectNode) object.path("Parent").path("Child");
JsonNode star = child.path("*");
child.remove("*");
Iterator<String> startFieldNames = star.fieldNames();
while (startFieldNames.hasNext()) {
String startFieldName = startFieldNames.next();
child.set(startFieldName, star.get(startFieldName));
}
System.out.println(mapper.writer().withDefaultPrettyPrinter().writeValueAsString(object));
添加回答
举报
0/150
提交
取消