Errors Caused by mixing org json and fast json, jsonfast

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.