gpt4 book ai didi

java - 从 Magento JSON 设计 Java 对象

转载 作者:行者123 更新时间:2023-11-30 08:15:06 24 4
gpt4 key购买 nike

下面是从 Magento REST API 调用返回的原始 JSON 字符串:

{  
"1":{
"entity_id":"1",
"website_id":"1",
"email":"john.oliver@hbo.com",
"group_id":"1",
"created_at":"2015-04-21 12:47:00",
"disable_auto_group_change":"0",
"prefix":null,
"firstname":"John",
"middlename":null,
"lastname":"Oliver",
"suffix":null,
"taxvat":null,
"created_in":"Admin"
},
"2":{
"entity_id":"2",
"website_id":"1",
"email":"beck.johnson@yahoo.com",
"group_id":"1",
"created_at":"2015-04-21 13:40:48",
"disable_auto_group_change":"0",
"prefix":null,
"firstname":"Beckie",
"middlename":null,
"lastname":"Johnson",
"suffix":null,
"taxvat":null,
"created_in":"Admin"
}
}

我想第一次构建 POJO,以下是类:

public class Customers {

private Map<String, Customer> customerMap;

public Map<String, Customer> getCustomerMap() {
return customerMap;
}

public void setCustomerMap(Map<String, Customer> customerMap) {
this.customerMap = customerMap;
}
}

public class Customer {
private String entity_id;
private String website_id;
private String email;
private String group_id;
private String created_at;
private String disable_auto_group_change;
private String prefix;
private String firstname;
private String middlename;
private String lastname;
private String suffix;
private String taxvat;
private String created_in;
public String getEntity_id() {
return entity_id;
}
public void setEntity_id(String entity_id) {
this.entity_id = entity_id;
}
public String getWebsite_id() {
return website_id;
}
public void setWebsite_id(String website_id) {
this.website_id = website_id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGroup_id() {
return group_id;
}
public void setGroup_id(String group_id) {
this.group_id = group_id;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getDisable_auto_group_change() {
return disable_auto_group_change;
}
public void setDisable_auto_group_change(String disable_auto_group_change) {
this.disable_auto_group_change = disable_auto_group_change;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getMiddlename() {
return middlename;
}
public void setMiddlename(String middlename) {
this.middlename = middlename;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getTaxvat() {
return taxvat;
}
public void setTaxvat(String taxvat) {
this.taxvat = taxvat;
}
public String getCreated_in() {
return created_in;
}
public void setCreated_in(String created_in) {
this.created_in = created_in;
}

}

然后,我尝试使用Gson进行解析:

Gson gson = new Gson();
Customers customers = gson.fromJson(response.getBody(), Customers.class);
System.out.println(customers.getCustomerMap());

但结果是null

我怀疑类(class)格式可能有误,有人可以建议什么是正确的格式吗?每组开头的数字 12 由 Magento 自动生成。

最佳答案

默认情况下这不起作用,因为 Gson 期望有一个名为 "customerMap" 的字段为了使用默认的反序列化。最简单的(不一定是最好的)解决方案就是简单地执行以下操作:

String jsonText = "{\"customerMap\":"+response.getBody()+"}";
Customers customers = gson.fromJson(jsonText, Customers.class);

通过在映射外部添加字段名称,Gson 知道要做什么,并且 println将输出,例如:

{1=Customer@2f56a6be, 2=Customer@61dd1c39}

不过,最好写成custom deserializer for your Customers class ,或者修改 Magneto 的输出以添加周围的 {"customerMap":}

<小时/>

不要尝试这样做:

Map<String, Customer> cust = gson.fromJson(jsonText, Map.class); 

这将实际上生成一个 Map类型 Map<String, LinkedTreeMap> ,其中LinkedTreeMap是一个内部 gson 类。

关于java - 从 Magento JSON 设计 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com