使用 GSON 或 Jackson 的 HashMap 到 POJO
HashMap to POJO using GSON or Jackson
我的转换代码:
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;
}
我的实体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;
}
}
我使用代码得到的结果:
{
"name" : "akshay",
"mobile" : 1234567890
}
但我想要这样的输出:
{
"name" : "akshay",
"mobile" : 1234567890,
"utility" : {
"soap" : "lux",
"noodles" : "maggi"
}
}
GSON没有映射SOAP和面条,因为POJO中没有对应的匹配对象。这就是我使用哈希图的原因。我想让GSON把所有不匹配的字段放到一个hashmap中。
一个值得注意的事情是实用程序下的键值对不是固定的取决于客户购买。我的意思是它可能像下面这样。
{
"name" : "akshay",
"mobile" : 1234567890,
"utility" : {
"toothpaste" : "colgate",
"noodles" : "maggi"
}
}
这样做就可以了:
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 忽略了肥皂和面条,因为它们的高度不正确。它们需要 children 的“效用”。
如果您希望按照您描述的特定方式解析“平面”JSON 结构(匹配字段被映射 1:1,不匹配的字段进入实用程序),您可以编写自定义(反)序列化器。有关示例,请参见此问题:GSON - Custom serializer in specific case。但是,我不推荐这样做,因为它最终可能会增加工作量。
我的转换代码:
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;
}
我的实体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;
}
}
我使用代码得到的结果:
{
"name" : "akshay",
"mobile" : 1234567890
}
但我想要这样的输出:
{
"name" : "akshay",
"mobile" : 1234567890,
"utility" : {
"soap" : "lux",
"noodles" : "maggi"
}
}
GSON没有映射SOAP和面条,因为POJO中没有对应的匹配对象。这就是我使用哈希图的原因。我想让GSON把所有不匹配的字段放到一个hashmap中。
一个值得注意的事情是实用程序下的键值对不是固定的取决于客户购买。我的意思是它可能像下面这样。
{
"name" : "akshay",
"mobile" : 1234567890,
"utility" : {
"toothpaste" : "colgate",
"noodles" : "maggi"
}
}
这样做就可以了:
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 忽略了肥皂和面条,因为它们的高度不正确。它们需要 children 的“效用”。
如果您希望按照您描述的特定方式解析“平面”JSON 结构(匹配字段被映射 1:1,不匹配的字段进入实用程序),您可以编写自定义(反)序列化器。有关示例,请参见此问题:GSON - Custom serializer in specific case。但是,我不推荐这样做,因为它最终可能会增加工作量。