java 从对象创建 Jackson ObjectNode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34180538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Hymanson ObjectNode from Object
提问by Chris
I need to add a new item to an existing ObjectNode
, given a key and a value. The value is specified as an Object
in the method sig and shouldbe one of the types that ObjectNode.set() accepts (String
, Integer
, Boolean
, etc). But I can't just do myObjectNode.set(key, value);
because value is just an Object
and of course I get a "not applicable for the arguments (String, Object)"error.
ObjectNode
给定一个键和一个值,我需要将一个新项目添加到现有的。该值被指定为Object
在该方法中SIG和应该是ObjectNode.set()接受(的类型之一String
,Integer
,Boolean
等等)。但我不能这样做,myObjectNode.set(key, value);
因为值只是一个Object
,当然我得到一个“不适用于参数(字符串,对象)”的错误。
My make-it-work solution is to create a function to check the instanceof
and cast it to create a ValueNode
:
我的让它工作的解决方案是创建一个函数来检查instanceof
并转换它以创建一个ValueNode
:
private static ValueNode getValueNode(Object obj) {
if (obj instanceof Integer) {
return mapper.createObjectNode().numberNode((Integer)obj);
}
if (obj instanceof Boolean) {
return mapper.createObjectNode().booleanNode((Boolean)obj);
}
//...Etc for all the types I expect
}
..and then I can use myObjectNode.set(key, getValueNode(value));
..然后我可以使用 myObjectNode.set(key, getValueNode(value));
There must be a better waybut I'm having trouble finding it.
一定有更好的方法,但我找不到它。
I'm guessing that there is a way to use ObjectMapper
but how isn't clear to me at this point. For example I can write the value out as a stringbut I need it as something I can set on my ObjectNode and needs to be the correct type (ie everything can't just be converted to a String).
我猜有一种方法可以使用,ObjectMapper
但此时我不清楚如何使用。例如,我可以将值作为字符串写出,但我需要它作为我可以在 ObjectNode 上设置的东西,并且需要是正确的类型(即所有东西都不能只是转换为字符串)。
回答by Alexey Gavrilov
Use ObjectMapper#convertValuemethod to covert object to a JsonNode instance. Here is an example:
使用ObjectMapper#convertValue方法将对象转换为 JsonNode 实例。下面是一个例子:
public class HymansonConvert {
public static void main(String[] args) {
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode root = mapper.createObjectNode();
root.set("integer", mapper.convertValue(1, JsonNode.class));
root.set("string", mapper.convertValue("string", JsonNode.class));
root.set("bool", mapper.convertValue(true, JsonNode.class));
root.set("array", mapper.convertValue(Arrays.asList("a", "b", "c"), JsonNode.class));
System.out.println(root);
}
}
Output:
输出:
{"integer":1,"string":"string","bool":true,"array":["a","b","c"]}
回答by Gili
Using the put()
methods is a lot easier:
使用这些put()
方法要容易得多:
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = mapper.createObjectNode();
root.put("name1", 1);
root.put("name2", "someString");
ObjectNode child = root.putObject("child");
child.put("name3", 2);
child.put("name4", "someString");