When fastJson encounters an upper-case field, fastjson encounters an upper-case field.

Source: Internet
Author: User

When fastJson encounters an upper-case field, fastjson encounters an upper-case field.

I encountered a headache in the project. When fastJson is used to deserialize the current Json, the object property is always null (for example,). You may know what the problem is at a glance, right! The problem is that the first letter of the returned field is capitalized. FastJson parses the value assigned to the object through setXxx () through reflection, which is why our javabean must raise the price for the getXxx () and setXxx () methods.
If you know where the problem is, you have to find a solution. The first thought was Baidu, but not after a while. Both the database and the server are not self-built, and it is unrealistic to change the database and server. The server only needs to consider on android, that is, to lower the first letter of the returned Json field, or change the fields in the data table. Well, here we will not sell the data. The solution is very simple. directly convert the Json string toLowerCase () to JsonObject, in this case, the javabean can all be processed in lower case.
Key code:JSONObject jObject = new JSONObject (resource. toLowerCase (Locale. getDefault ()));

{"Res": "1", "index_banner": [{"Ad_Name": "rolling Figure 1", "Ad_Link": "/huodong/fabuhui.html ", "Ad_Code": "/UpLoads/image/20140910/20140910172048 _31579.jpg"}, {"Ad_Name": "rolling switch five", "Ad_Link": "http://m.toptuu.com/Huodong/danye.html", "Ad_Code ": "/UpLoads/image/20140906/20140906084953 _94943.jpg"}, {"Ad_Name": "Mobile Phone rolling switch 4", "Ad_Link": "/Huodong/dingzhi.html", "Ad_Code ": "/UpLoads/image/20140906/20140906085053 _90516.jpg"}, {"Ad_Name": "Mobile Phone rolling Switch 3", "Ad_Link": "/Huodong/jiu.html", "Ad_Code ": "/UpLoads/image/20140906/20140906085138 _32961.jpg"}, {"Ad_Name": "Mobile Phone rolling Figure 2", "Ad_Link": "/huodong/jy.html", "Ad_Code ": "/UpLoads/image/20140910/20140910152954 _72484.jpg"}, {"Ad_Name": "switching between mobile phones 0", "Ad_Link": "/Huodong/xifeng.html", "Ad_Code ": "/UpLoads/image/20140830/20140830115112 _40561.jpg"}]}

The solution is simple, but it does take me a long time to figure it out. As long as it is a problem, it is worth recording and will always help others.


Filter extra fields in fastjson

SimplePropertyPreFilter filter = new SimplePropertyPreFilter (entity class. class, "field name", "field name ");
JSON. toJSONString (list, filter );
 
JSON parsing with fastjson

Syntax error pos 2 second keyword location format error

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.