- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要向现有的 ObjectNode
添加一个新项目,给定一个键和一个值。该值在方法 sig 中指定为 Object
,应该 是 ObjectNode.set() 接受的类型之一(String
,Integer
、Boolean
等)。但我不能只执行 myObjectNode.set(key, value);
因为 value 只是一个 Object
当然我得到一个 "not applicable for the参数(字符串,对象)” 错误。
我的成功解决方案是创建一个函数来检查 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
}
..然后我可以使用 myObjectNode.set(key, getValueNode(value));
一定有更好的方法,但我找不到它。
我猜想有一种方法可以使用 ObjectMapper
但目前我还不清楚如何使用。例如I can write the value out as a string但我需要它作为我可以在我的 ObjectNode 上设置的东西,并且需要是正确的类型(即不能将所有内容都转换为字符串)。
最佳答案
使用ObjectMapper#convertValue将对象转换为 JsonNode 实例的方法。这是一个例子:
public class JacksonConvert {
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);
}
}
输出:
{"integer":1,"string":"string","bool":true,"array":["a","b","c"]}
关于java - 从对象创建 Jackson ObjectNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180538/
我有以下 ObjectNode。 handlerObjectNode -> {"Info":{"Brand":{"BrandName":"TOP OF THE WORLD"}}} 我有另一个具有以下格
我正在使用 play2.2.1,现在遇到错误: com.fasterxml.jackson.databind.node.ObjectNode 无法转换为 org.codehaus.jackson.no
我的 @RequestBody ObjectNode objectNode 中的 JSON { "script": {"id":2,"nom":"tes","libellepr
我有一个通过 ObjectNode 获得的 JSON。 但是,我无法获取对象“LibellePrerequis”的ID使用的方法是否正确? 如果我这样做: String libelleprerequi
ObjectNode row = Json.newObject(); row.put("0", a); row.put("1", x); row.put("2", y); 现在我有了 list Lis
我需要向现有的 ObjectNode 添加一个新项目,给定一个键和一个值。该值在方法 sig 中指定为 Object,应该 是 ObjectNode.set() 接受的类型之一(String,Inte
我正在尝试设置代码来使用 jackson 创建节点树,然后可以使用该代码来编写 JSON 或 XML。我已经像这样手动创建了节点树: XmlMapper NodeMap = new XmlMapper
如何使用 Jackson 从字符串创建 ObjectNode? 我试过: ObjectNode json = new ObjectMapper().readValue("{}", ObjectNode
我有一个包含一些数据的 com.fasterxml JsonNode 对象。我需要对其数据进行一些操作。我在谷歌上搜索了答案,但没有得到正确的答案。你能建议我如何操作 JsonNode 数据吗?我还尝
我正在使用 Jackson 进行 JSON 解析。JsonNode 和 ObjectNode 有什么区别? 以及用于以字符串格式映射 JSON。 最佳答案 快速回答 JsonNode :抽象类,读取
我想扩展 ObjectNode,这样我就可以有几个辅助方法来从 JSON 中获取几个特定字段。例如 public class MyNode extends ObjectNode { public
我正在尝试增加 ObjectMapper 的重用,目前正在考虑移动以下代码: ObjectMapper mapper = new ObjectMapper(); SimpleModule module
我想从 json 中删除一些值。json格式是这样的: { "cod": "200", "message": 0.0135, "cnt": 40, "list": [
这个问题已经有答案了: How to directly write to a JSON object (ObjectNode) from ObjectMapper in Jackson JSON? (
你好,我有一些 com.fasterxml.jackson.databind.node.ObjectNode 形式的 Json 数据,我想将它存储在 MongoDB 中。 如何将 ObjectNode
我正在使用 jackson 将我的对象序列化为 json。我正在使用以下代码: ObjectMapper mapper = new ObjectMapper(); JsonNodeFactory no
首先谁能给我解释一下ObjectNode和JsonNode有什么区别,用在什么地方? 那如果我想把一个JSON字符串转成ObjectNode怎么办呢? 最佳答案 你可以这样做: ObjectMappe
我想使用 Swagger 为我的 Spring Boot API 提供 API 文档。我设法让 Springfox 2.3.0 正常工作,除了 Controller 返回 ObjectNode 之外,
我有这个代码: static String createRequestJson(String apiKey, String apiSecret) { JsonNodeFactory factory
假设我有以下映射到 Jackson 完整数据绑定(bind)的 Java 对象: public class Student implements Serializable{ private O
我是一名优秀的程序员,十分优秀!