Differences in use of Gson and Fastjson (Advantages of Fastjson)

Source: Internet
Author: User

I. JSON parsing method commonly used by Android

The JSON parsing methods commonly used in Android development are: Gson, Fastjson,jackson.
Because the Jackjson jar package will be relatively large (700+k), Gson and Fastjson function is basically enough, so has not been formally used Jackjson. It is Gson and fastjson that help me.

Second, the basic mode of use Gson
Jia Cheng object Fromat Mfromat =new Gson (). Fromjson (Jsonstringobject, Fromat.     Class); //Jia Cheng object Group linkedlist<fromat> list = new linkedlist<mainactivity.fromat> (); Type type = new typetoken<linkedlist<fromat>> () {}.gettype (); list = new Gson (). Fromjson (Jsonstringarray, type); //Generic Unified package requires a type to be passed in new typetoken<linkedlist<fromat>> () {}.gettype (); Fromjson (String json, type Typeoft) public <T> T fromjson(String json, Type typeoft)      
Fastjson
Resolves to an object Fromat Fastjsonobject = Json.parseobject (jsonobjectstring, Fromat.Class);Resolves to object group list<fromat> Fastjsonarray = Json.parsearray (jsonarraystring, Fromat.class); //Generic Unified package requires a type to be passed in or typereference can also be called type new Typereference<fromat> () {} new typereference& Lt Fromat> () {}.gettype () public static <T> T parseobject(String input, Type clazz, Feature ... Features) public static <T> T parseobject(String text, typereference<t> type , Feature ... features)              
Third, the advantages of Fastjson

maintainability > Correctness > Performance
To be honest, gson the JSON keyword when it encounters {} [], parsing it directly to a string error. Fastjson is able to parse
Demo Code:

String jsonobjectstring ="{\" status\ ": 0}";String jsonarraystring ="[{\" status\ ": 0}]";FastjsonString Fastjsonobject =Json.parseobject (Jsonobjectstring,String.class); LOG.D ("Test","Fastjson string {} =" + Fastjsonobject);String Fastjsonarray =Json.parseobject (Jsonarraystring,String.class); LOG.D ("Test","Fastjson string [] =" + Fastjsonarray);Gsontry {string gsonobject = new Gson (). Fromjson (jsonobjectstring, string.class); LOG.D ( "test",  "Gson string {} =" + Gsonobject);} catch (Exception e) {log.d ( "test", try {string Gsonarray = new Gson ( ). Fromjson (jsonarraystring, string.class); LOG.D ( "test",  "Gson string {} =" + Gsonarray);} catch (Exception e) {log.d ( "test", 

Result: D/test:fastjson String {} = {"status": 0} d/test:fastjson string [] = [{"Status": 0}] D/test:gson string {} = Com.goo Gle.gson.jsonsyntaxexception:java.lang.illegalstateexception:d/test:gson string [] = Com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:Expected a string but is Begin_array at line 1 col Umn 2 Path $

From:http://www.voidcn.com/blog/lckj686/article/p-6000736.html

Differences in use of Gson and Fastjson (Advantages of Fastjson)

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.