您需要改用ObjectMapper#valueToTree()。
这将构造等效的JSON树表示形式。功能与将值序列化为JSON并将JSON解析为树一样,但是效率更高。
如果不需要,则不需要将User
对象写出到JSON文件中。
public class MyClass {
private ObjectNode jsonObj;
public ObjectNode getJson() {
ObjectMapper mapper = new ObjectMapper();
// some code to generate the Object user...
JsonNode jsonNode = mapper.valueToTree(user);
if (jsonNode.isObject()) {
jsonObj = (ObjectNode) jsonNode;
return jsonObj;
}
return null;
}
}