Java 如何将 Map<String, Object> 反序列化为 POJO?

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

How to deserialize a Map<String, Object> into POJO?

javaserializationHymansongson

提问by Sampo

I have a Map<String, Object> which contains a deserialized form of JSON. I would like to deserialize this into the fields of a POJO.

我有一个 Map<String, Object> ,其中包含反序列化形式的 JSON。我想将其反序列化为 POJO 的字段。

I can perform this using Gson by serializing the Map into a JSON string and then deserializing the JSON string into the POJO, but this is inefficient (see example below). How can I perform this without the middle step?

我可以使用 Gson 执行此操作,方法是将 Map 序列化为 JSON 字符串,然后将 JSON 字符串反序列化为 POJO,但这效率低下(请参见下面的示例)。如果没有中间步骤,我如何执行此操作?

The solution should preferably use either Gson or Hymanson, as they're already in use by the project.

该解决方案最好使用 Gson 或 Hymanson,因为它们已被项目使用。

Example code:

示例代码:

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

public class Test {
    public static void main(String[] args) {
        Map<String, Object> innermap = new HashMap<String, Object>();
        innermap.put("number", 234);
        innermap.put("string", "bar");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("number", 123);
        map.put("string", "foo");
        map.put("pojo2", innermap);

        Gson gson = new Gson();

        // How to perform this without JSON serialization?
        String json = gson.toJson(map);
        MyPojo pojo = gson.fromJson(json, MyPojo.class);

        System.out.println(pojo);
    }
}


class MyPojo {
    private int number;
    private String string;
    private MyPojo pojo2;

    @Override
    public String toString() {
        return "MyPojo[number=" + number + ", string=" + string + ", pojo2=" + pojo2 + "]";
    }
}

采纳答案by andersschuller

Using Gson, you can turn your Map into a JsonElement, which you can then parse in exactly the same way using fromJson:

使用 Gson,您可以将 Map 转换为 a JsonElement,然后您可以使用完全相同的方式对其进行解析fromJson

JsonElement jsonElement = gson.toJsonTree(map);
MyPojo pojo = gson.fromJson(jsonElement, MyPojo.class);

This is similar to the toJsonmethod that you are already using, except that it avoids writing the JSON String representation and then parsing that JSON String back into a JsonElement before converting it to your class.

这类似于toJson您已经在使用的方法,不同之处在于它避免编写 JSON 字符串表示,然后在将其转换为您的类之前将该 JSON 字符串解析回 JsonElement。

回答by Micha? Ziober

In Hymansonyou can use convertValuemethod. See below example:

Hymanson您可以使用convertValue方法。见下面的例子:

mapper.convertValue(map, MyPojo.class);

回答by user1191976

You can directly construct MyPojo by giving it the map. Something like

您可以通过给它映射来直接构建 MyPojo。就像是

MyPojo pojo = new MyPojo(map);

And declaring an according constructor :

并声明一个相应的构造函数:

public MyPojo(Map<String, Object> map){
  this.number=map.get("number");
  this.string=map.get("string");
  this.pojo2=new MyPojo(); // I don't know if this is possible
  this.pojo2.string=map.get("pojo2").get("string");
  this.pojo2.number=map.get("pojo2").get("number");
}

回答by Jignesh Gothadiya

You can use Hymanson library for convert map object to direct POJO.

您可以使用 Hymanson 库将地图对象转换为直接 POJO。

Map<String, String> map = new HashMap<>();
map.put("id", "5");
map.put("name", "Bob");
map.put("age", "23");
map.put("savings", "2500.39");

ObjectMapper mapper = new ObjectMapper();
MyPojo pojo = mapper.convertValue(map, MyPojo.class);