昨天遇到參數key大小寫不一致導致校驗簽名失敗的問題,查了很長時間才找到原因。看了一下FastJson源碼,發現JSON.toObject中轉換成對象的時候會忽略大小寫。 所以,當使用了JSON.toObject將json轉成Java對象後,再用JSON.toObject轉成json,key值就變了 ...
昨天遇到參數key大小寫不一致導致校驗簽名失敗的問題,查了很長時間才找到原因。看了一下FastJson源碼,發現JSON.toObject中轉換成對象的時候會忽略大小寫。
所以,當使用了JSON.toObject將json轉成Java對象後,再用JSON.toObject轉成json,key值就變了。
寫個方法驗證一下:
public class Person {
private String nickName;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JsonToObject {
public static void main(String[] args) {
JSONObject json1 = new JSONObject();
json1.put("nickName", "shawn1");
System.out.println(json1);
Person p1 = json1.toJavaObject(Person.class);
System.out.println(p1.getNickName());
JSONObject json11 = (JSONObject)JSON.toJSON(p1);
System.out.println(json11);
System.out.println("--------------------");
JSONObject json2 = new JSONObject();
json2.put("nickname", "shawn2");
System.out.println(json2);
json2.toJavaObject(Person.class);
Person p2 = json2.toJavaObject(Person.class);
System.out.println(p2.getNickName());
JSONObject json22 = (JSONObject)JSON.toJSON(p2);
System.out.println(json22);
System.out.println("--------------------");
JSONObject json3 = new JSONObject();
json3.put("nIcknAme", "shawn3");
System.out.println(json3);
json3.toJavaObject(Person.class);
Person p3 = json3.toJavaObject(Person.class);
System.out.println(p3.getNickName());
JSONObject json33 = (JSONObject)JSON.toJSON(p3);
System.out.println(json33);
System.out.println("--------------------");
}
}
輸出:
{"nickName":"shawn1"}
shawn1
{"nickName":"shawn1"}
--------------------
{"nickname":"shawn2"}
shawn2
{"nickName":"shawn2"}
--------------------
{"nIcknAme":"shawn3"}
shawn3
{"nickName":"shawn3"}
--------------------
JSON.toObject中轉換成對象的時候忽略了大小寫,可以增強健壯性,但Java類是區分大小寫的,如果有兩個僅僅帶小寫不同的屬性,可能就出問題了。
給Person增加一個屬性再試一下:
public class Person {
private String nickName;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
private String nickname;
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JsonToObject {
public static void main(String[] args) {
JSONObject json1 = new JSONObject();
json1.put("nickName", "shawn1");
System.out.println(json1);
Person p1 = json1.toJavaObject(Person.class);
System.out.println("nickName:"+p1.getNickName()+"\t nickname:"+p1.getNickname());
JSONObject json11 = (JSONObject)JSON.toJSON(p1);
System.out.println(json11);
System.out.println("--------------------");
JSONObject json2 = new JSONObject();
json2.put("nickname", "shawn2");
System.out.println(json2);
json2.toJavaObject(Person.class);
Person p2 = json2.toJavaObject(Person.class);
System.out.println("nickName:"+p2.getNickName()+"\t nickname:"+p2.getNickname());
JSONObject json22 = (JSONObject)JSON.toJSON(p2);
System.out.println(json22);
System.out.println("--------------------");
JSONObject json3 = new JSONObject();
json3.put("nIcknAme", "shawn3");
System.out.println(json3);
json3.toJavaObject(Person.class);
Person p3 = json3.toJavaObject(Person.class);
System.out.println("nickName:"+p3.getNickName()+"\t nickname:"+p3.getNickname());
JSONObject json33 = (JSONObject)JSON.toJSON(p3);
System.out.println(json33);
System.out.println("--------------------");
}
}
輸出:
{"nickName":"shawn1"}
nickName:shawn1 nickname:null
{"nickName":"shawn1"}
--------------------
{"nickname":"shawn2"}
nickName:null nickname:shawn2
{"nickname":"shawn2"}
--------------------
{"nIcknAme":"shawn3"}
nickName:null nickname:shawn3
{"nickname":"shawn3"}
--------------------
這個結果符合你的預期嗎?
所以,用JSON.toObject的時候還是得慎重。
以上。
https://wangxuan.me/tech/2023/06/02/FastJson-convert-json-to-Java-Object-field-ignore-case.html