Jackson在解析返回的json字符串时始终报错,纠结很久之后才找到原因,原来是是由于json字符串中的字母都是首字母大写,导致jackson找不到相应的KEY。
在项目中经常使用从服务器获取的数据,有时从服务器获取的json字符串中的首字母是大写的,使用Jackson解析是就会报一下错误:
Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "XXX" (Class xxxx), not marked as ignorable .
异常信息告诉说XXX属性字段在类型xxx中没有找到,也没有标记为忽略。
但是在你的bean中确实存在这个字段,而只不过是这个字段的首字母是大写而已。
解决这个问题只需要在属性字段上面加上@JsonProperty();即可。
1 public class CommissionItemVo implements Serializable{ 2 3 /** 4 * 5 */ 6 private static final long serialVersionUID = 1L; 7 8 private int docId; 9 @JsonProperty("CName") 10 private String CName; 11 @JsonProperty("CPhone") 12 private String CPhone; 13 @JsonProperty("BlockName") 14 private String BlockName; 15 @JsonProperty("SeeTime") 16 private String SeeTime; 17 @JsonProperty("Cost") 18 private String Cost; 19 @JsonProperty("EndSettleTime") 20 private String EndSettleTime; 21 22 public int getDocId() { 23 return docId; 24 } 25 26 27 28 public void setDocId(int docId) { 29 this.docId = docId; 30 } 31 32 33 34 public String getCName() { 35 return CName; 36 } 37 38 39 40 public void setCName(String cName) { 41 CName = cName; 42 } 43 44 45 46 public String getCPhone() { 47 return CPhone; 48 } 49 50 51 52 public void setCPhone(String cPhone) { 53 CPhone = cPhone; 54 } 55 56 57 58 public String getBlockName() { 59 return BlockName; 60 } 61 62 63 64 public void setBlockName(String blockName) { 65 BlockName = blockName; 66 } 67 68 69 70 public String getSeeTime() { 71 return SeeTime; 72 } 73 74 75 76 public void setSeeTime(String seeTime) { 77 SeeTime = seeTime; 78 } 79 80 81 82 public String getCost() { 83 return Cost; 84 } 85 86 87 88 public void setCost(String cost) { 89 Cost = cost; 90 } 91 92 93 94 public String getEndSettleTime() { 95 return EndSettleTime; 96 } 97 98 99 100 public void setEndSettleTime(String endSettleTime) { 101 EndSettleTime = endSettleTime; 102 } 103 104 105 106 public static long getSerialversionuid() { 107 return serialVersionUID; 108 } 109 110 111 112 @Override 113 public String toString() { 114 return "CommissionItemVo [docId=" + docId + ", CName=" + CName + ", CPhone=" + CPhone + ", BlockName=" + BlockName + ", SeeTime=" + SeeTime + ", Cost=" + Cost + ", EndSettleTime=" + EndSettleTime + "]"; 115 } 116 117 118 119 }
时间: 2024-09-29 05:43:49