在使用fastjson的时候,一般默认对象转换成的字符串key的首字母是小写的,而json字符串转换成java bean对象的时候,json字符串怎么写的,封装javabean的时候就要怎么写,这样才能成功set值给javabean。如果需要将javabean转换json字符串时key的首字母大写可以使用fastjson 的注解 @JSONField,例如:
/**
* @author chenjianwei
* @date 2019年11月7日
*/
public class TestModel {
@JSONField(name="ICCID")
String ICCID;
@JSONField(name="IMEI")
String IMEI;
@JSONField(name="IP")
String IP;
@JSONField(name="MAC")
String MAC;
/**
* @return the iCCID
*/
public String getICCID() {
return this.ICCID;
}
/**
* @param iCCID the iCCID to set
*/
public void setICCID(String iCCID) {
this.ICCID = iCCID;
}
/**
* @return the iMEI
*/
public String getIMEI() {
return this.IMEI;
}
/**
* @param iMEI the iMEI to set
*/
public void setIMEI(String iMEI) {
this.IMEI = iMEI;
}
/**
* @return the iP
*/
public String getIP() {
return this.IP;
}
/**
* @param iP the iP to set
*/
public void setIP(String iP) {
this.IP = iP;
}
/**
* @return the mAC
*/
public String getMAC() {
return this.MAC;
}
/**
* @param mAC the mAC to set
*/
public void setMAC(String mAC) {
this.MAC = mAC;
}
}
@Test
public void fastJsonTest() {
TestModel aa = new TestModel();
aa.setICCID("ASDF");
aa.setIMEI("ASDF");
aa.setIP("ASDF");
aa.setMAC("ASDF");
String aaa = JSONObject.toJSONString(aa);
System.out.println(aaa);
}
{"ICCID":"ASDF","IMEI":"ASDF","IP":"ASDF","MAC":"ASDF"}