Errors Caused by mixing org json and fast json, jsonfast
1. Different valuesWhen the obtained key does not exist:Org json throws an exception. fast json returns null.Example:Com. alibaba. fastjson. JSONObject fastJson = new com. alibaba. fastjson. JSONObject (); String name = fastJson. getString ("name"); // fastJson normal output name: nullSystem. out. println ("name:" + name); org. json. JSONObject orgJson = new org. json. JSONObject (); // throw an exception: org. json. JSONException: JSONObject ["name"] not found. name = orgJson. getString ("name"); System. out. println ("name:" + name );2. put org json in fast jsonThis operation is abnormal and will put null jsonExample:Org. json. JSONObject orgJsonAddress = new org. json. JSONObject (); orgJsonAddress. put ("country", "China"); orgJsonAddress. put ("province", "Henan"); orgJsonAddress. put ("city", "Zhengzhou"); com. alibaba. fastjson. JSONObject fastJson = new com. alibaba. fastjson. JSONObject (); fastJson. put ("name", "Qingjiang Li"); fastJson. put ("sex", 1); fastJson. put ("address", orgJsonAddress); System. out. println (fastJson. toJSONString (); abnormal output: {"address" :{}, "sex": 1, "name": "Qingjiang Li "}3. put fast json in org jsonThis operation is normalExample:Com. alibaba. fastjson. JSONObject fastJsonAddress = new com. alibaba. fastjson. JSONObject (); fastJsonAddress. put ("country", "China"); fastJsonAddress. put ("province", "Henan"); fastJsonAddress. put ("city", "Zhengzhou"); org. json. JSONObject orgJson = new org. json. JSONObject (); orgJson. put ("name", "Qingjiang Li"); orgJson. put ("sex", 1); orgJson. put ("address", fastJsonAddress); System. out. println (orgJson. toString (); the output is normal: {"address": {"country": "China", "province": "Henan", "city": "Zhengzhou "}, "sex": 1, "name": "Qingjiang Li "}Conclusion:In the project, try to use the same JSON type. If you have to use it all, pay attention to it to avoid errors caused by habits.