[Gson] [1] Gson Introduction

Source: Internet
Author: User

1. What is JSON

1. Definition]

JSONJavaScript Object Notation) is a JavaScript Object Notation used to store and exchange text information syntax, similar to XML, but smaller and more flexible than XML. It is an independent language and self-descriptive, so it is easier to understand.

JSON uses JavaScript syntax to describe data objects, but SJON is still independent of the language platform. The JSON parser and JSON library support many different programming languages.


2. Example]

{

"Employees ":[

{"FristName": "Li", "lastName": "min "},

{"FristName": "Zhang", "lastName": "feng "},

{"FristName": "Li", "lastName": "yunfeng "},

]

}

In this example, the object "employee" contains three object arrays. Each object in the array represents a person's name information.


3. Syntax]

JSON syntax is a subset of JavaScript Object Notation Syntax:

1) Data in key-value pairs

2) data is separated by commas (,).

3) Brackets save objects

4) square brackets Save the Array


4. Understanding]

1) Data in key-value pairs

Here, "data" can be understood as "data value". For example, if the key-Value Pair "firstName": "Li", the data value is Li, it is equivalent to the String firstName = "Li" of the java statement ". The data can be an integer, a floating point number, a string in double quotation marks), a Boolean value true or false, an array in square brackets, and an object in the right curly brackets) and null.


2) data is separated by commas (,).

For example, "firstName": "Li", "lastName": "min", the two key-value pairs are separated by commas, which is equivalent to defining two variables:

String firstName = "Li ";

String lastName = "min ";


3) Brackets save objects

For example, {"firstName": "Li", "lastName": "min"} is an object, which can be understood in this way using java

Private class Employee

{

String firstName = null;

String lastName = null;


Public Employee (String firstName, String lastName)

{

This. firstName = firstName;

This. lastName = lastName;

}

}

{"FirstName": "Li", "lastName": "min"} is equivalent to new Employee ("Li", "min ")


4) square brackets Save the Array

For example, "employees": [{"fristName": "Li", "lastName": "min" },{ "fristName": "Li", "lastName ": "yunfeng"}] is an array, which can be understood in java.

Employee employee1 = new Employee ("Li", "min ");

Employee employee2 = new Employee ("Li", "yunfeng ");

Employee [] employees = new Employee [] {Employee 1, empolyee2 };


5. Remarks]

For basic knowledge about JSON, see w3school, URL http://www.w3school.com.cn/json/index.asp


Ii. JSON parser

If you are smart, do you want to convert the JSON structure into a java object? The answer is yes. This is what we will focus on in this article. Since JSON is a string, you can parse the string by yourself. Of course, do as little as possible to re-invent the wheel unless the wheel of the new invention is more reasonable. Existing wheels:

1) The official JSON website provides the official Java Implementation org. json) for parsing and serialization.

2) StringTree: the parser features "simple". JSONReader is responsible for parsing and JSONWriter is responsible for serialization.

3) JSEL, not only can parse, but also can process JSON comments, and even some simple expressions, the interface is simple and the performance is good.

In addition, there are famous jackson and GSON.


For the comparison between the advantages and disadvantages of GSON and other Resolvers, please test and verify it yourself.


Iii. Introduction to GSON

1. GSON Introduction]

GSON is a class library used for conversion between Json and Java objects. Even if the source code of the Java class has not yet been implemented, GSON is an open-source project with the URL http://code.google.com/p/google-gson.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1544404215-0.gif "/> here the reader may be confused, Java class source code has not been implemented, through GSON can be converted to this Java object? Here is an example:

1) there is a JSON string {name: 'greetings ', source: 'guest '}

2) If the JSON structure is converted to a Demo object, two fields name and source are usually defined for the Demo class. However, the following Demo class definition does not implement the name and source fields, as follows:

Public class Demo

{

}

3) running the test method will find that it can work normally.

Public static void main (String [] args)

{

String s = "{name: 'greetings ', source: 'guest '}";

Gson gson = new Gson ();

Demo demo = gson. fromJson (s, Demo. class );

}


2. Gson goals]

You can use the create () method of new Gson () or GsonBuilder to create a GSON object. Since the GSON object does not save the state, you can serialize and deserialize JSON without restrictions. The following functions can be easily implemented using GSON objects:

1) Output concise and easy-to-read JSON code

2) support for any complex objects

3) allows users to convert special expressions

4) provides as simple as possible assembly operations, just like the operator constructor and toString () method, switching between JSON and Java objects.


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.