简体   繁体   English

如何在 Jackson 中将 JSON 字符串解析为 JsonNode?

[英]How to parse a JSON string into JsonNode in Jackson?

It should be so simple, but I just cannot find it after being trying for an hour.应该就是这么简单,可是试了一个小时还是找不到。

I need to get a JSON string, for example, {"k1":v1,"k2":v2} , parsed as a JsonNode .我需要获取一个 JSON 字符串,例如{"k1":v1,"k2":v2} ,解析为JsonNode

JsonFactory factory = new JsonFactory();
JsonParser jp = factory.createJsonParser("{\"k1\":\"v1\"}");
JsonNode actualObj = jp.readValueAsTree();

gives

java.lang.IllegalStateException: No ObjectCodec defined for the parser, can not deserialize JSON into JsonNode tree

A slight variation on Richards answer but readTree can take a string so you can simplify it to:理查兹答案略有变化,但readTree可以采用字符串,因此您可以将其简化为:

ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree("{\"k1\":\"v1\"}");

You need to use an ObjectMapper :您需要使用ObjectMapper

ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use mapper.getFactory() instead
JsonParser jp = factory.createJsonParser("{\"k1\":\"v1\"}");
JsonNode actualObj = mapper.readTree(jp);

Further documentation about creating parsers can be found here .可以在此处找到有关创建解析器的更多文档。

A third variant:第三种变体:

ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readValue("{\"k1\":\"v1\"}", JsonNode.class);

Richard's answer is correct.理查德的回答是正确的。 Alternatively you can also create a MappingJsonFactory (in org.codehaus.jackson.map ) which knows where to find ObjectMapper .或者,您也可以创建一个MappingJsonFactory (在org.codehaus.jackson.map ),它知道在哪里可以找到ObjectMapper The error you got was because the regular JsonFactory (from core package) has no dependency to ObjectMapper (which is in the mapper package).你得到的错误是因为常规的JsonFactory (来自core包)不依赖于ObjectMapper (它在mapper包中)。

But usually you just use ObjectMapper and do not worry about JsonParser or other low level components -- they will just be needed if you want to data-bind parts of stream, or do low-level handling.但通常您只使用ObjectMapper而不必担心JsonParser或其他低级组件——如果您想对流的部分进行数据绑定或进行低级处理, JsonParser需要它们。

import com.github.fge.jackson.JsonLoader;
JsonLoader.fromString("{\"k1\":\"v1\"}")
== JsonNode = {"k1":"v1"}

New approach to old question.旧问题的新方法。 A solution that works from java 9+一个适用于 java 9+ 的解决方案

ObjectNode agencyNode = new ObjectMapper().valueToTree(Map.of("key", "value"));

is more readable and maintainable for complex objects.对于复杂的对象,更具可读性和可维护性。 Ej Ej

Map<String, Object> agencyMap = Map.of(
        "name", "Agencia Prueba",
        "phone1", "1198788373",
        "address", "Larrea 45 e/ calligaris y paris",
        "number", 267,
        "enable", true,
        "location", Map.of("id", 54),
        "responsible", Set.of(Map.of("id", 405)),
        "sellers", List.of(Map.of("id", 605))
);
ObjectNode agencyNode = new ObjectMapper().valueToTree(agencyMap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM