首页 技术 正文
技术 2022年11月13日
0 收藏 467 点赞 3,636 浏览 1826 个字

前面有一篇关于  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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918