"Android Development experience" super-useful JSON parsing tool--gson Project usage Introduction

Source: Internet
Author: User
Tags tojson

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

In the previous article, we briefly introduced the JSON data format and described how to use the JSON tool that comes with Android to complete the generation and parsing of JSON data. But like I said, the tool class comes in too hard! So this article will introduce a very good JSON parsing tool, which is Google's Open source project Gson.

This time we do not introduce gson inside the common class, because the common few classes are very simple, we start directly to use, to see how powerful Gson!

of course, if you use a third-party project, we are sure to import the jar package, a search a lot of ha.

Give the test model code

Package Com.example.jsondemo;public class Person {private String name;private int age;private Birthday birthday;public St Ring GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Birthday Getbirthday () {return Birthday;} public void Setbirthday (Birthday Birthday) {this.birthday = Birthday;} Public person () {super ();} Public person (String name, int age, Birthday Birthday) {super (); this.name = Name;this.age = Age;this.birthday = Birthday;} @Overridepublic String toString () {return "person [name=" + name + ", age=" + Age + ", birthday=" + Birthday + "]";}}


Package com.example.jsondemo;/** *  * @ClassName: Com.example.jsondemo.Birthday * @Description: Birthday class * @author Zhaokaiqiang * @date 2014-11-26 a.m. 10:29:47 *  */public class Birthday {private int year;private int month;private int D ay;public int GetYear () {return year;} public void Setyear (int.) {this.year = year;} public int GetMonth () {return month;} public void setmonth (int month) {this.month = month;} public int GetDay () {return day;} public void Setday (Int. day) {this.day = day;} Public Birthday () {super ();} Public Birthday (int. int, int month, int day) {super (); this.year = Year;this.month = Month;this.day = day;} @Overridepublic String toString () {return "Birthday [year=" + year + ", month=" + month + ", day=" + day+ "]";}}



1.Object and JSON conversion to each other

Let's start with the simplest single object.

Here is the code implementation

/** * JSON generation and parsing of single objects */public void Objecttojson () {person p = new person ("Zhaokaiqiang", New Birthday (1992, 1, 19)); Gson Gson = new Gson (); String jsonstring = Gson.tojson (p); LOG.D (TAG, "---------single object Generation--------"); LOG.D (TAG, jsonstring); Person person = Gson.fromjson (jsonstring, Person.class); LOG.D (TAG, "---------single object Parsing--------"); LOG.D (TAG, person.tostring ());}

This code is how concise, beautiful ah!

In the previous article, if you want to build a simple object, we also need to specify the key, and then set value, since the use of Gson, we no longer have these troubles! And in order to illustrate the strength of Gson, I also deliberately set an object for person birthday, if the use of the JSON tool, it does not bother to die. But using Gson, there is no need for these concerns at all. Look at the following conversion results, a tojson (), completely solve the problem, great!

11-26 16:16:38.392:d/gsontest (16931):---------single object Generation--------11-26 16:16:38.392:d/gsontest (16931): {"Name": " Zhaokaiqiang "," Birthday ": {" Day ": +," month ": 1," Year ": 1992}," Age ": 22}11-26 16:16:38.392:d/gsontest (16931):------- --Single object parsing--------11-26 16:16:38.392:d/gsontest (16931): Person [Name=zhaokaiqiang, age=22, Birthday=birthday [year= 1992, Month=1, Day=19]]

2. Conversion of collections to JSON

Gson can be converted to a collection in addition to being easily converted to a single object.

Here is a code that generates the JSON and finishes parsing:

/** * JSON generation and parsing of collection objects */public void Objectstojson () {Gson Gson = new Gson (); person person = new Person ("Zhaokaiqiang", 22,new Birthday (1992, 1, 19)); arraylist<person> ArrayList = new arraylist<person> (), Arraylist.add (person), arraylist.add (person); Arraylist.add (person); String jsonstring = Gson.tojson (arrayList); LOG.D (TAG, "---------Collection Object Generation--------"); LOG.D (TAG, jsonstring); Type type = new Typetoken<arraylist<person>> () {}.gettype (); arraylist<person> persons = Gson.fromjson (jsonstring, type); LOG.D (TAG, "---------Set object parsing--------"); LOG.D (TAG, persons.tostring ());}

This is the result of the conversion .

---------Collection Object generated--------[{"Name": "Zhaokaiqiang", "Birthday": {"Day": +, "month": 1, "Year": 1992}, "Age": 22},{"name": " Zhaokaiqiang "," Birthday ": {" Day ": +, ' month ': 1, ' Year ': 1992}, ' Age ': 22},{' name ': ' Zhaokaiqiang ', ' birthday ': {" Day " : +, "month": 1, "Year": 1992}, "Age": $}]---------Collection object parsing--------[person [Name=zhaokaiqiang, age=22, Birthday=birthday [year=1992, Month=1, day=19]], person [Name=zhaokaiqiang, age=22, Birthday=birthday [year=1992, Month=1, day=19]], person [Name=zhaokaiqiang, age=22, Birthday=birthday [year=1992, Month=1, day=19]]

Remember when we used JSON to do something like that, it would be unthinkable, but it would be so simple to use Gson to do that.

When we use the Gson.tojson () method, we can put any object or collection into it and we can transform it into JSON format perfectly.

If we want to convert the JSON-formatted data into the object we want, we just need to call Gson.fromjson ().

But at the time of the conversion, the second parameter has two forms, one is class<?>, and this is when you convert a single object, we need to pass in the object. class to tell the converter what type to convert. If we need to convert a collection, we need to pass an instance that implements the type interface, and the syntax is just like the code above, and the collection type has a specific object to change.


"Android Development experience" super-useful JSON parsing tool--gson Project usage Introduction

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.