简体   繁体   English

HashMap 到 POJO 使用 GSON 或 Jackson

[英]HashMap to POJO using GSON or Jackson

My code for conversion:我的转换代码:

public MyEntity convert(){
            HashMap<String,String> map = new HashMap<>();
            map.put("name","akshay");
            map.put("mobile","xxxxxxx");
            map.put("soap","lux");
            map.put("noodles","maggi");
            Gson gson = new Gson();
            JsonElement jsonElement = gson.toJsonTree(map);
            MyEntity pojo = gson.fromJson(jsonElement, MyEntity.class);
            System.out.println(gson.toJson(pojo));
            return pojo;
       }

My Entity class:我的实体 class:

public class MyEntity {
    private String name;
    private int mobile;
    private HashMap<String,String> utility;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMobile() {
        return mobile;
    }

    public void setMobile(int mobile) {
        this.mobile = mobile;
    }

    public HashMap<String, String> getUtility() {
        return utility;
    }

    public void setUtility(HashMap<String, String> utility) {
        this.utility = utility;
    }
}

What I am getting using my code:我使用我的代码得到了什么:

{
    "name" : "akshay",
    "mobile" : 1234567890
}

But I want output like:但我想要 output 喜欢:

{
    "name" : "akshay",
    "mobile" : 1234567890,
    "utility" : {
        "soap" : "lux",
        "noodles" : "maggi"
    }
}

GSON does not map SOAP and noodles because there is no corresponding matching object in POJO. GSON没有map SOAP和面条因为在POJO66中没有对应的匹配ZA8CFDE6331BD59EB2AC96F8911C4B6。 That's why I took hashmap.这就是我选择 hashmap 的原因。 I want GSON to put all the unmatching fields into a hashmap.我希望 GSON 将所有不匹配的字段放入 hashmap。

One noticeable thing the key-value pair under utility is not fixed depends upon customer purchase.效用下的键值对不固定的一件值得注意的事情取决于客户购买。 I mean it might be something else like below.我的意思是它可能是下面的其他东西。

{
    "name" : "akshay",
    "mobile" : 1234567890,
    "utility" : {
        "toothpaste" : "colgate",
        "noodles" : "maggi"
    }
}

This will do the trick:这可以解决问题:

    HashMap<String, Object> map = new HashMap<>();
    map.put("name", "akshay");
    map.put("mobile", "xxxxxxx");
    HashMap<String, String> utility = new HashMap<>();
    utility.put("soap", "lux");
    utility.put("noodles", "maggi");
    map.put("utility", utility);

GSON ignores soap and noodles since they are not on the right level. GSON 忽略 soap 和面条,因为它们不在正确的级别上。 They need to be children of "utility".他们需要成为“效用”的孩子。

If you would like a "flat" JSON structure to be parsed in a specific way as you describe (matching fields are mapped 1:1, unmatched fields go into utility), you can write a custom (de)serializer.如果您希望以您描述的特定方式解析“平面” JSON 结构(匹配字段以 1:1 映射,不匹配字段 go 进入实用程序),您可以编写自定义(反)序列化程序。 See this question for an example: GSON - Custom serializer in specific case .有关示例,请参见此问题: GSON - Custom serializer in specific case However, I would not recommend this as it probably ends up being more work.但是,我不建议这样做,因为它可能最终需要更多的工作。

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

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