Getting Started with Android Gson and using the Gsonformat plugin

Source: Internet
Author: User

Gson is the Java class library that Google officially provides to convert between Java objects and JSON.
When I was using eclipse to develop Android, I didn't use gson often, but I used native jsonobject parsing, and recently switched to Android Studio to work with the powerful plugins inside the IDE, using Gson to greatly improve the efficiency of development. This blog will introduce the basic use of Gson, with practical cases to realize the convenience of Gson development experience.

1. JSON data to parse

{  "Code":0,  "msg":"Carousel Meeting gets successful",  "Records":[    {      "Joinnumber":3,      "ID":Ten,      "Startdatestr":"2016-10-26 09:00 Wednesday",      "Theme":"2016 overseas high-level crowd gathering"    },    {      "Joinnumber":3,      "ID":1,      "Startdatestr":"2016-10-24 08:00 Monday",      "Theme":"2016 Suzhou Medical Conference"    }  ]}

2, create the corresponding JavaBean:MeetingData.java

 PackageCom.leohan.gsondemo;ImportJava.util.List;/** * Created by Leo on 16/3/14. * * Public  class meetingdata {    /** * code:0 * msg: Carousel Meeting gets successful * records: [{"Joinnumber": 3, "id": Ten, "Startdatestr": "2016-10-26 09:00 Wednesday" , "theme": "2016 overseas high-level crowd gathering"},{"Joinnumber": 3, "id": 1, "Startdatestr": "2016-10-24 08:00 Monday", "theme": "2016 degree Suzhou Medical Conference"} ]     */    Private intCodePrivateString msg;/** * joinnumber:3 * id:10 * startdatestr:2016-10-26 09:00 Wednesday * theme:2016 annual overseas high-level crowd gathering */< /c4>    PrivateList<recordsentity> Records; Public void Setcode(intCode) { This. Code = Code; } Public void setmsg(String msg) { This. msg = msg; } Public void Setrecords(List<recordsentity> Records) { This. Records = records; } Public int GetCode() {returnCode } PublicStringgetmsg() {returnMsg } PublicList<recordsentity>GetRecords() {returnRecords } Public Static  class recordsentity {        Private intJoinnumber;Private intIdPrivateString Startdatestr;PrivateString theme; Public void Setjoinnumber(intJoinnumber) { This. Joinnumber = Joinnumber; } Public void setId(intID) { This. id = ID; } Public void Setstartdatestr(String startdatestr) { This. startdatestr = Startdatestr; } Public void SetTheme(String theme) { This. theme = Theme; } Public int Getjoinnumber() {returnJoinnumber; } Public int getId() {returnId } PublicStringGetstartdatestr() {returnSTARTDATESTR; } PublicStringGettheme() {returnTheme }    }}

First analyze the JSON data to parse, it contains a field named "Records" of the Jsonarray.

Thus, we generate the corresponding JavaBean of the JSON data, and inside the records array corresponding to the internal class recordsentity.

3. Using Gson to parse JSON data

 Gson gson = new Gson();        MeetingData meetingData = gson.fromJson(jsonStr, MeetingData.class);        List<MeetingData.RecordsEntity> records = meetingData.getRecords();

Run the program and print the records object:

This completes the parsing of the JSON object. Other uses of Gson, such as dealing with generics, do not unfold.

4. Accelerate development with Gsonformat plug-in
In the second step, a new JavaBean is created that corresponds to the JSON data to be parsed. Are there any plugins that directly assist in generating JavaBean? Introducing Gsonformat...

So how does the Gsonformat plugin work?

    • preferences–> plugins–> Search Gsonformat Installation
    • Create a new JavaBean after installation, operation:
    • Fill in the Popup interface with the JSON data you want to parse


      Click Ok,gsonformat to automatically help us create this javabean.

SOURCE Click to download

Getting Started with Android Gson and using the Gsonformat plugin

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.