Use of the Android JSON Parsing Library-Gson and Fast-json

Source: Internet
Author: User
Tags tojson

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript.
JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, and so on).
These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (network transfer rate).

Gson is a Java class library that has been officially launched by Google to convert JSON and Java objects

Fast-json Ali launched the library for Android platform, the performance of the Java implementation of the JSON parsing and generator, is said to be powerful and speed beyond all existing libraries

Gson usage:

1. Parsing Jsonobject,jsonarray

2. Convert the JSON entity to JSON data

Project Import three pack Fastjson, Gson,volley

//get data from Douban, inhttp://www.bejson.com/Parsing View DataPrivate voidGetData () {stringrequest request=NewStringrequest (URL,NewListener<string>() {@Override Public voidonresponse (String arg0) {log.i (TAG, arg0);//after the acquisition is complete,http://www.bejson.com/Parsing View DataDealData (arg0); }    }, NewErrorlistener () {@Override Public voidonerrorresponse (Volleyerror arg0) {//TODO auto-generated Method Stub        }    }); Newvolley (). Newrequestqueue (Getapplicationcontext ()). Add (request);//working with DataPrivate voiddealData (String result) {Gson Gson=NewGson (); Book Book= Gson.fromjson (result, book.class);//Data Turn entityLOG.I (TAG, Book.gettitle () + ";"            + book.getpublisher () + ";" + book.gettags (). Size () + ";" );}

Private String TAG = "Carloz"; Private String url = "https://api.douban.com/v2/book/1220562"; @Overrideprotectedvoid  OnCreate (Bundle savedinstancestate) {    super. OnCreate (savedinstancestate);    Setcontentview (r.layout.activity_jsonparse);        GetData (); }

Book belongs to the entity class, see Code GitHub for details

Output results

08-10 21:19:22.321:i/carloz (24911): {"rating": {"Max": Ten, "numraters": 340, "average": "7.0", "Min": 0}, "subtitle": "", " Author ": [" [Day] piece Shan Gong Yi "]," pubdate ":" 2005-1 "," tags ": [{" Count ": 133," name ":" The piece Shan Gong Yi "," title ":" Sheet Shan Gong yi "},{" Count ":", "" Name ":" Japan "," title ":" Japan "},{" Count ":" Japanese Literature "," title ":" Japanese Literature ","},{":" A "," Name ":" Novel "," title ":" Novel "},{" Count ": 32, "Name": "Full Moon night Moby Dick Now", "title": "Full Moon night Moby Dick"},{"Count": "Name": "Love", "title": "Love"},{"Count": 8, "name": "Pure Love", "title": "Pure Love"} , {"Count": 8, "name": "Foreign Literature", "title": "Foreign Literature"}], "Origin_title": "", "image": "http:\/\/img3.douban.com\/mpic\/ S1747553.jpg "," Binding ":" Paperback "," Translator ": [" Yu Man "]," Catalog ":" \ n "," pages ":" "", "Images": {"small": "http:\/\/ Img3.douban.com\/spic\/s1747553.jpg "," large ":" Http:\/\/img3.douban.com\/lpic\/s1747553.jpg "," Medium ":" Http:\/\ /img3.douban.com\/mpic\/s1747553.jpg "}," alt ":" http:\/\/book.douban.com\/subject\/1220562\/"," id ":" 1220562 "," Publisher ":" Qingdao Publishing House "," Isbn10 ":" 7543632608 "," Isbn13 ":" 9787543632608 "," title ":" Full Moon night Moby Dick Now "," url ":" http:\/\/ Api.douban.com\/v2\/book\/1220562 "," Alt_title ":", "Author_intro": "," Summary ":" That year was a year of listening to Mozart, bass fishing and family breakdown. " When it comes to family rupture, the mother blames herself for not finding a good man, but the father thought it was the mother who was fascinated by the fox, but the problem was the father. "," Price ":" 15.00 yuan "}08-10 21:19:22.371:i/carloz (24911): Full Moon night white whale now; Qingdao publishing house; 8;

Fast-json

 Public classFastjsonparseactivityextendsActivity {PrivateString TAG = "Carloz"; PrivateString url = "https://api.douban.com/v2/book/1220562"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_fast_json_parse);    GetData (); }    Private voidGetData () {stringrequest request=NewStringrequest (URL,NewListener<string>() {@Override Public voidonresponse (String arg0) {log.i (TAG, arg0);            DealData (arg0); }        }, NewErrorlistener () {@Override Public voidonerrorresponse (Volleyerror arg0) {//TODO auto-generated Method Stub            }        }); Newvolley (). Newrequestqueue (Getapplicationcontext ()). Add (Request); }//JSON data to Java objectPrivate voiddealData (String result) { Book Book= Json.parseobject (result, book.class); LOG.I (TAG, Book.gettitle ()+ "; "                + book.getpublisher () + ";" + book.gettags (). Size () + ";" ); }}

Operation Result:

08-10 21:43:11.991:i/carloz (28779): {"rating": {"Max": Ten, "numraters": 340, "average": "7.0", "Min": 0}, "subtitle": "", " Author ": [" [Day] piece Shan Gong Yi "]," pubdate ":" 2005-1 "," tags ": [{" Count ": 133," name ":" The piece Shan Gong Yi "," title ":" Sheet Shan Gong yi "},{" Count ":", "" Name ":" Japan "," title ":" Japan "},{" Count ":" Japanese Literature "," title ":" Japanese Literature ","},{":" A "," Name ":" Novel "," title ":" Novel "},{" Count ": 32, "Name": "Full Moon night Moby Dick Now", "title": "Full Moon night Moby Dick"},{"Count": "Name": "Love", "title": "Love"},{"Count": 8, "name": "Pure Love", "title": "Pure Love"} , {"Count": 8, "name": "Foreign Literature", "title": "Foreign Literature"}], "Origin_title": "", "image": "http:\/\/img3.douban.com\/mpic\/ S1747553.jpg "," Binding ":" Paperback "," Translator ": [" Yu Man "]," Catalog ":" \ n "," pages ":" "", "Images": {"small": "http:\/\/ Img3.douban.com\/spic\/s1747553.jpg "," large ":" Http:\/\/img3.douban.com\/lpic\/s1747553.jpg "," Medium ":" Http:\/\ /img3.douban.com\/mpic\/s1747553.jpg "}," alt ":" http:\/\/book.douban.com\/subject\/1220562\/"," id ":" 1220562 "," Publisher ":" Qingdao Publishing House "," Isbn10 ":" 7543632608 "," Isbn13 ":" 9787543632608 "," title ":" Full Moon night Moby Dick Now "," url ":" http:\/\/ Api.douban.com\/v2\/book\/1220562 "," Alt_title ":", "Author_intro": "," Summary ":" That year was a year of listening to Mozart, bass fishing and family breakdown. " When it comes to family rupture, the mother blames herself for not finding a good man, but the father thought it was the mother who was fascinated by the fox, but the problem was the father. "," Price ":" 15.00 yuan "}08-10 21:43:12.101:i/carloz (28779): Full Moon night white whale now; Qingdao publishing house; 8;
//object to JSON data
Private voiddealData (String result) {List<Book> books = Json.parseobject (result,NewTypereference<list<book>>(){}); Book Book1=NewBook (); Book1.settitle ("Hello Carloz"); Book Book2=NewBook (); Book2.settitle ("Hello Carloz"); Book Book3=NewBook (); Book3.settitle ("Hello Carloz"); Json.tojson (BOOK1); //Book1 to JSON dataJson.tojson (books);}

Use of the Android JSON Parsing Library-Gson and Fast-json

Related Article

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.