使用新的对象值更新 JsonNode
Update JsonNode with new object values
我正在获取 JsonNode,其中一个键的值为 null。需要用对象更新这个值。
m return 的值将如下所示,其中注释为空,需要更新此注释值。
{
"a":"aaaaaa",
"b":"bbbbbb",
"notes":null,
}
代码尝试了 m 作为 JsonNode 出现的地方:
return seasonsAdapter.callSeasonsJsonAPI(seasonId, hunterId, uuid).map(m -> {
String notes=AppJsonUtil.getJsonAsString(seasonsNotesDto);
ObjectNode objectNode = (ObjectNode) m;
ObjectNode objectNode1=objectNode.put("notes", notes);
objectNode1.put("notesText", seasonsNotesDto.getNotesText());
objectNode1.put("notesTime", seasonsNotesDto.getNotesTime());
objectNode1.put("notesTitle", seasonsNotesDto.getNotesTitle());
return m;
});
结果:
{
"a":"aaaaaa",
"b":"bbbbbb",
"notes": "{\"notesText\":\"this is my new note 1 \",\"notesTitle\":\"new note 1 Title\",\"notesTime\":1640865242125}",
"notesText": "this is my new note 1 ",
"notesTime": 1640865242125,
"notesTitle": "new note 1 Title"
}
预期:
{
"a":"aaaaaa",
"b":"bbbbbb",
"notes": {
"notesText": "this is my new note 1 ",
"notesTime": 1640865242125,
"notesTitle": "new note 1 Title"
}
}
我已经弄清楚需要做什么了。检查下面的代码
ObjectMapper mapper = new ObjectMapper();
ObjectNode obj = (ObjectNode) m;
if (seasonsNotesDto!=null)
{
ObjectNode notes = mapper.createObjectNode();
notes.put("notesTitle", seasonsNotesDto.getNotesTitle());
notes.put("notesText", seasonsNotesDto.getNotesText());
notes.put("notesTime", seasonsNotesDto.getNotesTime());
obj.set("notes", notes);
}
return m;
我正在获取 JsonNode,其中一个键的值为 null。需要用对象更新这个值。
m return 的值将如下所示,其中注释为空,需要更新此注释值。
{
"a":"aaaaaa",
"b":"bbbbbb",
"notes":null,
}
代码尝试了 m 作为 JsonNode 出现的地方:
return seasonsAdapter.callSeasonsJsonAPI(seasonId, hunterId, uuid).map(m -> {
String notes=AppJsonUtil.getJsonAsString(seasonsNotesDto);
ObjectNode objectNode = (ObjectNode) m;
ObjectNode objectNode1=objectNode.put("notes", notes);
objectNode1.put("notesText", seasonsNotesDto.getNotesText());
objectNode1.put("notesTime", seasonsNotesDto.getNotesTime());
objectNode1.put("notesTitle", seasonsNotesDto.getNotesTitle());
return m;
});
结果:
{
"a":"aaaaaa",
"b":"bbbbbb",
"notes": "{\"notesText\":\"this is my new note 1 \",\"notesTitle\":\"new note 1 Title\",\"notesTime\":1640865242125}",
"notesText": "this is my new note 1 ",
"notesTime": 1640865242125,
"notesTitle": "new note 1 Title"
}
预期:
{
"a":"aaaaaa",
"b":"bbbbbb",
"notes": {
"notesText": "this is my new note 1 ",
"notesTime": 1640865242125,
"notesTitle": "new note 1 Title"
}
}
我已经弄清楚需要做什么了。检查下面的代码
ObjectMapper mapper = new ObjectMapper();
ObjectNode obj = (ObjectNode) m;
if (seasonsNotesDto!=null)
{
ObjectNode notes = mapper.createObjectNode();
notes.put("notesTitle", seasonsNotesDto.getNotesTitle());
notes.put("notesText", seasonsNotesDto.getNotesText());
notes.put("notesTime", seasonsNotesDto.getNotesTime());
obj.set("notes", notes);
}
return m;