Getting started with Android Gson and using the GsonFormat plug-in

Source: Internet
Author: User

Getting started with Android Gson and using the GsonFormat plug-in

Gson is a Java class library officially provided by Google to convert Java objects and JSON objects.
When I used Eclipse to develop Android, I didn't often use Gson. Instead, I used Native JSONObject parsing. Recently I switched to Android Studio to use powerful plug-ins in the IDE, gson greatly improves the development efficiency. This blog will introduce the basic usage of Gson, and use cases to experience the convenient development experience of Gson.

1. Json data to be parsed

{"Code": 0, "msg": "Carousel meeting succeeded", "records": [{"joinNumber": 3, "id": 10, "startDateStr ": "2016 Wednesday", "theme": "overseas high-level group gathering in" },{ "joinNumber": 3, "id": 1, "startDateStr ": "2016 Monday", "theme": "Suzhou Medical conference"}]}

2. Create the corresponding JavaBean: MeetingData. java

Package com. leohan. gsondemo; import java. util. list;/*** Created by Leo on 16/3/14. */public class MeetingData {/*** code: 0 * msg: carousel meeting succeeded * records: [{"joinNumber": 3, "id": 10, "startDateStr": "2016-10-26 Wednesday", "theme": "overseas high-level group gathering in 2016" },{ "joinNumber": 3, "id": 1, "startDateStr": "2016-10-24 Monday", "theme": "2016 Suzhou Medical conference"}] */private int code; private String msg;/*** joinNumber: 3 * id: 10 * startDateStr: 2016 Wednesday * theme: Overseas high-level group gathering in */private List
  
   
Records; public void setCode (int code) {this. code = code;} public void setMsg (String msg) {this. msg = msg;} public void setRecords (List
   
    
Records) {this. records = records;} public int getCode () {return code;} public String getMsg () {return msg;} public List
    
     
GetRecords () {return records;} public static class RecordsEntity {private int joinNumber; private int id; private String startDateStr; private String theme; public void setJoinNumber (int joinNumber) {this. joinNumber = joinNumber;} public void setId (int id) {this. id = id;} public void setStartDateStr (String startDateStr) {this. startDateStr = startDateStr;} public void setTheme (String theme) {this. theme = theme;} public int getJoinNumber () {return joinNumber;} public int getId () {return id;} public String getStartDateStr () {return startDateStr;} public String getTheme () {return theme ;}}}
    
   
  

First, analyze the Json data to be parsed. It contains a JsonArray with the field name "records.

Therefore, we generate the JavaBean corresponding to the Json data and create the RecordsEntity internal class corresponding to the records array.

3. parse Json data using Gson

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

Run the program and print the records object:

So far, Json Object Parsing has been completed. Other Gson usage, such as processing generics, will not be elaborated.

4. Use GsonFZ plugin? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + rPJSmF2YUJlYW61xLLlvP7E2KO/keys + JmhlbGxpcDs8L3A + keys/keys =" here write picture description "src =" http://www.bkjia.com/uploads/allimg/160319/0419513O9-2.png "title =" \ "/> Create a JavaBean after installation, operation:
Enter the Json data to be parsed in the pop-up interface.

JavaBean.

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.