Java json转model

31 篇文章 0 订阅
20 篇文章 0 订阅

前面有一篇关于  json的转换类的工具:java json转换(一)_正怒月神的博客-CSDN博客_jsonconvert

但是有一个情况。

由于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的实现: java json转换(一)_正怒月神的博客-CSDN博客_jsonconvert
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值