Java json转model

前面有一篇关于  json的转换类的工具:http://blog.csdn.net/hanjun0612/article/details/77891569

但是有一个情况。

由于java需要属性小写开头。

所以针对以下的json字符串,转换model时会出问题

{
"ResponseResult": true,
"ResponseMsg": "success",
"ResponseCode": 200,
"Data": [{
"W_Id": 6,
"CompanyId": 444,
"CompanyName": "AMERICAN CARGO EXPRESS",
"W_Name": "AMERICAN CARGO EXPRESS",
"W_Address": "70 E SUNRISE HIGHTWAY, SUITE 602 VALLEY STREAM, NY 11581, USA",
"W_Longitude": null,
"W_Latitude": null,
"W_UsableArea": null,
"IsDelete": 0,
"W_Contact": null,
"W_Phone": null,
"W_Remark": null,
"CreateTime": "2017-05-23T00:00:00"
}]
}

这里,我直接给出解决方案:使用

@JsonProperty("ResponseCode")
public class BaseModelAPI<T> {
@JsonProperty("ResponseResult")
private Boolean responseResult;
@JsonProperty("ResponseMsg")
private String responseMsg;
@JsonProperty("ResponseCode")
private Integer responseCode;
@JsonProperty("Data")
private T data;
public Boolean getResponseResult() {
return responseResult;
} public void setResponseResult(Boolean responseResult) {
this.responseResult = responseResult;
} public String getResponseMsg() {
return responseMsg;
} public void setResponseMsg(String responseMsg) {
this.responseMsg = responseMsg;
} public Integer getResponseCode() {
return responseCode;
} public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode;
} public T getData() {
return data;
} public void setData(T data) {
this.data = data;
} }

public class Warehouse {
private Integer w_Id;
private Integer companyId; public Integer getW_Id() {
return w_Id;
} public void setW_Id(Integer w_Id) {
this.w_Id = w_Id;
} public Integer getCompanyId() {
return companyId;
} public void setCompanyId(Integer companyId) {
this.companyId = companyId;
} }

转换例子:

BaseModelAPI<List<Warehouse>> result = JsonConvert.fromJson(msg.obj.toString(),new TypeReference<BaseModelAPI<List<Warehouse>>>(){});
BaseModelAPI<User> result = JsonConvert.fromJson(msg.obj.toString(),new TypeReference<BaseModelAPI<User>>(){});

JsonConvert的实现:http://blog.csdn.net/hanjun0612/article/details/77891569

上一篇:将Excel生成实体类


下一篇:iOS端JSON转Model链式编程框架SuperKVC使用方法与原理