12. Gson Simple Notes

Source: Internet
Author: User

I. Introduction to BASIC knowledge

1. Gson is a Java class library provided by Google for mapping between Java objects and JSON data. You can turn a JSON string into a Java object, or vice versa.

2. JSON (JavaScript Object Notation) is a lightweight data interchange format. JSON takes a completely language-independent text format, which makes JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate.

At the beginning of 21st century, Douglas Crockford looked for a simple data interchange format that could exchange data between servers. The universal data Exchange language was XML at the time, but Douglas Crockford thought that XML generation and parsing were too cumbersome, so he proposed a simplified format, json.

JSON specifications are very simple, with only one page hundreds of words can be said clearly, and Douglas Crockford claims that this specification never need to upgrade, because the provisions of the provision.

1) The data is separated by commas (",").

2) The map is represented by a colon (":").

3) a set (array) of side data is expressed in square brackets ("[]").

4) The Set of Mappings (objects) are represented by curly braces ("{}").

Second, the demo project

2.1 Project Structure diagram

The code in 2.2 mainactivity is as follows:

 PackageCom.example.administrator.testdemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportCom.google.gson.Gson;ImportCom.google.gson.reflect.TypeToken;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsActivity {PrivateGson Gson =NewGson (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Jsonobject Jsonobject=Generatajsonobjectdata (); Suitmodellistbo Suitmodellistbo= Gson.fromjson (Jsonobject.tostring (),NewTypetoken<suitmodellistbo>() {}.gettype ()); //Convert JSON-type data into bean type dataList<ModelBO> modellist =suitmodellistbo.modlelist; String Modelliststr= Gson.tojson (modellist);//converting bean type data to JSON type datalist<modelbo> childmodelbolist = Gson.fromjson (Modelliststr,NewTypetoken<arraylist<modelbo>>() {}.gettype ()); //Convert JSON-type data into bean type data    }    //generate jsonobject type of data    Privatejsonobject Generatajsonobjectdata () {jsonobject jsonobject=NewJsonobject (); Try{jsonobject JsonObject1=NewJsonobject (); Jsonobject1.put ("Status", 1); Jsonobject1.put ("Channelid", "Public1"); Jsonobject1.put ("IconUrl", "" "); Jsonobject1.put ("Modelversion", 1); Jsonobject1.put ("Iconaction", "" "); Jsonobject1.put ("Label", "Wealth"); Jsonobject1.put ("Childmodeltree", "" "); Jsonobject1.put ("id", 11); Jsonobject1.put ("Childmodellist", "12,13,14,15,16,17,18,19,20"); Jsonobject1.put ("Name", "Fortune"); Jsonobject1.put ("Phonesys", "Android"); Jsonobject1.put ("Action", "" "); Jsonobject1.put ("Redmark", "" "); Jsonobject1.put ("RedirectURL", "" "); Jsonobject JsonObject2=NewJsonobject (); Jsonobject2.put ("Status", 1); Jsonobject2.put ("Channelid", "Public1"); Jsonobject2.put ("IconUrl", "" "); Jsonobject2.put ("Modelversion", 1); Jsonobject2.put ("Iconaction", "" "); Jsonobject2.put ("Label", "Flowpay"); Jsonobject2.put ("Childmodeltree", "" "); Jsonobject2.put ("id", 12); Jsonobject2.put ("Childmodellist", "" "); Jsonobject2.put ("Name", "Traffic po"); Jsonobject2.put ("Phonesys", "Android"); Jsonobject2.put ("Action", "" "); Jsonobject2.put ("Redmark", "" "); Jsonobject2.put ("RedirectURL", "http://bao.e.189.cn/portal/rechargeFlowByCoin.do"); Jsonobject JsonObject3=NewJsonobject (); Jsonobject3.put ("Status", 1); Jsonobject3.put ("Channelid", "Public1"); Jsonobject3.put ("IconUrl", "" "); Jsonobject3.put ("Modelversion", 1); Jsonobject3.put ("Iconaction", "" "); Jsonobject3.put ("Label", "Phonepackage"); Jsonobject3.put ("Childmodeltree", "" "); Jsonobject3.put ("id", 13); Jsonobject3.put ("Childmodellist", "" "); Jsonobject3.put ("Name", "Package query"); Jsonobject3.put ("Phonesys", "Android"); Jsonobject3.put ("Action", "" "); Jsonobject3.put ("Redmark", "" "); Jsonobject3.put ("RedirectURL", "" "); Jsonarray modlelist=NewJsonarray ();            Modlelist.put (JSONOBJECT1);            Modlelist.put (JSONOBJECT2);            Modlelist.put (JSONOBJECT3); Jsonobject.put ("MSG", "Success"); Jsonobject.put ("Modlelist", modlelist); Jsonobject.put ("Result", "0"); } Catch(jsonexception e) {e.printstacktrace (); }        returnJsonobject; }}

The code in 2.3 Modelbo is as follows:

 PackageCom.example.administrator.testdemo; Public classModelbo { Public LongID;  PublicString Channelid;  PublicString name;  PublicString IconUrl;  PublicString Phonesys;  Public Longmodelversion;  PublicString Action;  PublicString Redmark;  PublicString iconaction;  PublicString label;  PublicString RedirectURL;  Public Longstatus;  PublicString Childmodeltree;  PublicString childmodellist;  PublicModelbo () {} PublicModelbo (LongID, string channelid, string name, String iconUrl, String Phonesys,Longmodelversion, String action, String Redmark, String iconaction, String label, String RedirectURL,Longstatus, String Childmodeltree, String childmodellist) {         This. ID =ID;  This. Channelid =Channelid;  This. Name =name;  This. ICONURL =IconUrl;  This. Phonesys =Phonesys;  This. modelversion =modelversion;  This. Action =Action;  This. Redmark =Redmark;  This. iconaction =iconaction;  This. Label =label;  This. RedirectURL =RedirectURL;  This. Status =status;  This. Childmodeltree =Childmodeltree;  This. childmodellist =childmodellist; }}

The code in 2.4 Suitmodellistbo is as follows:

 Package Com.example.administrator.testdemo; Import java.util.List;  Public class SUITMODELLISTBO {    public list<modelbo> modlelist;       Public Long timeStamp;      Public int result;      Public String msg;}

12. Gson Simple Notes

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.