首先需要下载包:jackson-databind-2.0.5.jar jackson-annotations-2.0.5.jar jackson-core-2.0.5.jar,网址在:http://wiki.fasterxml.com/JacksonDownload 或 https://github.com/FasterXML
JSON:
{
"code": "0",
"data": [
{
"AttachCount": "0",
"MFL_Creator": "SFU00000000001",
"MFL_ID": "MFL20141105008",
"MFL_Title": "管理员_444",
"MFN_ID": "MFN20110904002",
"MFV_ID": "MFV20110904001",
"MIN_GetTime": "2014-12-16",
"MIN_ID": "MIN201412160030",
"MWF_Name": "计经类申请报告",
"SFU_Name": "管理员"
},
{
"AttachCount": "0",
"MFL_Creator": "SFU00000000001",
"MFL_ID": "MFL20141211012",
"MFL_Title": "标题标题标题标题",
"MFN_ID": "MFN20090825014",
"MFV_ID": "MFV20090825003",
"MIN_GetTime": "2014-12-11",
"MIN_ID": "MIN201412110019",
"MWF_Name": "部门发文",
"SFU_Name": "管理员"
}
],
"message": "操作成功"
}
mapper:
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(Include.NON_NULL);
方法1:Jackson注解(最优方案)
说明:
@JsonProperty("NAME") 顾名思义,就是显示指定字段的别名,不管是输入还是输出都是这个名字。
扩展:(一般不用没影响)@JsonAutoDetect(JsonMethod.FIELD)这个的意思是指解析字段,如果不这样设置,有兴趣的朋友可以试一下,会输出两套东西,类似{name:'',NAME:''},也就是说字段和getter方法都解析了,所以需要制定只解析字段名,忽略方法。还有一种方法就是需要一行行的在所有getter上加上@JsonIgnore,如果字段多就累死了。
public class FlowResultBean implements Serializable{
private String code;
private String message;
private List data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
}
public static class WaitInfoBean{
@JsonProperty("AttachCount")
private String AttachCount;
@JsonProperty("MFL_Creator")
private String MFL_Creator;
@JsonProperty("MFL_ID")
private String MFL_ID;
@JsonProperty("MFL_Title")
private String MFL_Title;
@JsonProperty("MFN_ID")
private String MFN_ID;
@JsonProperty("MFV_ID")
private String MFV_ID;
@JsonProperty("MIN_GetTime")
private String MIN_GetTime;
@JsonProperty("MIN_ID")
private String MIN_ID;
@JsonProperty("MWF_Name")
private String MWF_Name;
@JsonProperty("SFU_Name")
private String SFU_Name;
public String getAttachCount() {
return AttachCount;
}
public void setAttachCount(String AttachCount) {
this.AttachCount = AttachCount;
}
public String getMFL_Creator() {
return MFL_Creator;
}
public void setMFL_Creator(String mFL_Creator) {
MFL_Creator = mFL_Creator;
}
public String getMFL_ID() {
return MFL_ID;
}
public void setMFL_ID(String mFL_ID) {
MFL_ID = mFL_ID;
}
public String getMFL_Title() {
return MFL_Title;
}
public void setMFL_Title(String MFL_Title) {
this.MFL_Title = MFL_Title;
}
public String getMFN_ID() {
return MFN_ID;
}
public void setMFN_ID(String mFN_ID) {
MFN_ID = mFN_ID;
}
public String getMFV_ID() {
return MFV_ID;
}
public void setMFV_ID(String mFV_ID) {
MFV_ID = mFV_ID;
}
public String getMIN_GetTime() {
return MIN_GetTime;
}
public void setMIN_GetTime(String mIN_GetTime) {
MIN_GetTime = mIN_GetTime;
}
public String getMIN_ID() {
return MIN_ID;
}
public void setMIN_ID(String mIN_ID) {
MIN_ID = mIN_ID;
}
public String getMWF_Name() {
return MWF_Name;
}
public void setMWF_Name(String mWF_Name) {
MWF_Name = mWF_Name;
}
public String getSFU_Name() {
return SFU_Name;
}
public void setSFU_Name(String sFU_Name) {
SFU_Name = sFU_Name;
}
}
方法2:把private String Version;改成public String Version;
注解不需要了. 应该是jackson默认先找public的属性, 如果没有就根据setVersion取属性(根据java命名规范自然就返回首字母小写的属性名)
方法3:把json串转小写后解析
result = mapper.readValue(json.toLowerCase(Locale.getDefault()), FlowResultBean.class);//小写的话bean的属性也小写
方法4:
1、这个方法的作用就是将json格式的数据转换成数组格式(使用fastJson)。
2、假设有Person这个类,有json类型数据jsonStr = [{"name":"张三","age":"1"},{"name":"李四","age":"4"}],
那么List lists = json.parseArray(jsonStr, Person.class);lists就可以接收jsonStr了
---------------------
建议阅读本文,有增加。
原文:https://blog.csdn.net/u011309801/article/details/42007969