Java 将 JsonNode 转换为 POJO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19711695/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:36:10  来源:igfitidea点击:

Convert JsonNode into POJO

javajsonHymanson

提问by Alexandre

This may seem a little unusual, but I am looking for an efficient way to transform/map a JsonNodeinto a POJO.

这可能看起来有点不寻常,但我正在寻找一种有效的方法将 a 转换/映射JsonNodePOJO.

I store some of my Model's information in json files and I have to support a couple of version of my model.

我将我的一些模型信息存储在 json 文件中,并且我必须支持我的模型的几个版本。

What I do is load the json file in memory in a JsonNode, apply a couple of versioning strategies to make it match the latest version of my Model.

我所做的是将 json 文件加载到 JsonNode 的内存中,应用几个版本控制策略以使其与我的模型的最新版本相匹配。

    ObjectMapper mapper = new ObjectMapper();
    BufferedReader fileReader = new BufferedReader(new FileReader(projPath));

    JsonNode rootNode = mapper.readTree(fileReader);

    //Upgrade our file in memory
    applyVersioningStrategy(rootNode);

    ProjectModel project = mapJsonNodeToProject(rootNode);

Unless there's a faster way to do it, I will probably end up simply manually applying the JsonNodesto my Model

除非有更快的方法来做到这一点,否则我最终可能会简单地手动将JsonNodes应用于我的模型

采纳答案by icedtrees

In Hymanson 2.4, you can convert as follows:

在 Hymanson 2.4 中,您可以进行如下转换:

MyClass newJsonNode = jsonObjectMapper.treeToValue(someJsonNode, MyClass.class);

where jsonObjectMapperis a Hymanson ObjectMapper.

jsonObjectMapperHyman逊在哪里ObjectMapper



In older versions of Hymanson, it would be

在旧版本的 Hymanson 中,它将是

MyClass newJsonNode = jsonObjectMapper.readValue(someJsonNode, MyClass.class);

回答by Eric Barr

This should do the trick:

这应该可以解决问题:

mapper.readValue(fileReader, MyClass.class);

I say should because I'm using that with a String, not a BufferedReaderbut it should still work.

我说应该是因为我将它与 a 一起使用String,而不是 aBufferedReader但它应该仍然有效。

Here's my code:

这是我的代码:

String inputString = // I grab my string here
MySessionClass sessionObject;
try {
    ObjectMapper objectMapper = new ObjectMapper();
    sessionObject = objectMapper.readValue(inputString, MySessionClass.class);

Here's the official documentation for that call: http://Hymanson.codehaus.org/1.7.9/javadoc/org/codehaus/Hymanson/map/ObjectMapper.html#readValue(java.lang.String, java.lang.Class)

这是该调用的官方文档:http: //Hymanson.codehaus.org/1.7.9/javadoc/org/codehaus/Hymanson/map/ObjectMapper.html#readValue(java.lang.String, java.lang.Class)

You can also define a custom deserializer when you instantiate the ObjectMapper: http://wiki.fasterxml.com/HymansonHowToCustomDeserializers

您还可以在实例化时定义自定义反序列化器ObjectMapperhttp: //wiki.fasterxml.com/HymansonHowToCustomDeserializers

Edit: I just remembered something else. If your object coming in has more properties than the POJOhas and you just want to ignore the extras you'll want to set this:

编辑:我只是想起了别的东西。如果传入的对象的属性比 has 多,POJO而您只想忽略要设置的额外内容:

    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Or you'll get an error that it can't find the property to set into.

或者你会得到一个错误,它找不到要设置的属性。

回答by llambda

String jsonInput = "{ \"hi\": \"Assume this is the JSON\"} ";
com.fasterxml.Hymanson.databind.ObjectMapper mapper =
    new com.fasterxml.Hymanson.databind.ObjectMapper();
MyClass myObject = objectMapper.readValue(jsonInput, MyClass.class);

If your JSON input in has more properties than your POJO has and you just want to ignore the extras in Hymanson 2.4, you can configure your ObjectMapper as follows. This syntax is different from older Hymanson versions.(If you use the wrong syntax, it will silently do nothing.)

如果您的 JSON 输入具有比 POJO 多的属性,并且您只想忽略 Hymanson 2.4 中的额外内容,则可以按如下方式配置 ObjectMapper。此语法与较旧的 Hymanson 版本不同。(如果你使用了错误的语法,它会默默地什么都不做。)

mapper.disable(com.fasterxml.Hymanson.databind.DeserializationFeature.FAIL_ON_UNK??NOWN_PROPERTIES);

回答by mumrah

If you're using org.codehaus.Hymanson, this has been possible since 1.6. You can convert a JsonNode to a POJO with ObjectMapper#readValue: http://Hymanson.codehaus.org/1.9.4/javadoc/org/codehaus/Hymanson/map/ObjectMapper.html#readValue(org.codehaus.Hymanson.JsonNode, java.lang.Class)

如果您正在使用 org.codehaus.Hymanson,这从 1.6 开始就已经成为可能。您可以使用以下命令将 JsonNode 转换为 POJO ObjectMapper#readValuehttp://Hymanson.codehaus.org/1.9.4/javadoc/org/codehaus/Hymanson/map/ObjectMapper.html#readValue(org.codehaus.Hymanson.JsonNode, java. lang.Class)


    ObjectMapper mapper = new ObjectMapper();
    JsonParser jsonParser = mapper.getJsonFactory().createJsonParser("{\"foo\":\"bar\"}");
    JsonNode tree = jsonParser.readValueAsTree();
    // Do stuff to the tree
    mapper.readValue(tree, Foo.class);