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)