[英]How serialize an object as a value to ObjectNode in Jackson?
I'm using jackson
to serialize my object as json. 我正在使用jackson
将对象序列化为json。 I'm using following code: 我正在使用以下代码:
ObjectMapper mapper = new ObjectMapper();
JsonNodeFactory nodeFactory = new JsonNodeFactory(false);
ObjectNode resNode = new ObjectNode(nodeFactory);
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(ApplicationVersion.class, new SingleApplicationSerializer());
mapper.registerModule(simpleModule);
JsonNode appObject = mapper.valueToTree(appVersion);
resNode.put("status", true);
resNode.put("appObject", appObject); //This put method is deprecated.
But I saw the put
method with JsonNode
overload is deprecated.Why following overload of put
method is deprecated: 但是我不赞成使用带有JsonNode
重载的put
方法。 JsonNode
不建议使用以下put
方法的重载:
public JsonNode put(String fieldName, JsonNode value);
What is the alternative for that? 有什么替代方法?
Also I tried this: 我也试过这个:
String jsonStr = mapper.writeValueAsString(appVersion);
resNode.put("status", true);
resNode.put("appObject", jsonStr);
But this method puts double quote ( "
) around appObject
value as a string value. 但是此方法将appObject
值的双引号( "
)用作字符串值。
They added a new method for these actions. 他们为这些操作添加了新方法。 ObjectNode.put(String, JsonNode)
Try instead resNode.set("appObject", appObject);
试一试resNode.set("appObject", appObject);
. 。
不推荐使用ObjectNode.put()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.