Download and deploy GSON
GSON's GitHub page address: https://github.com/google/gson
Before using the GSON API to work, you need to download the library (jar file) and include it in the classpath. The library, along with the source code and Java documentation, can be downloaded from http://code.google.com/p/google-gson/downloads/list. After downloading, add gson-<version>.jar to the classpath. For those who prefer to use Maven management dependencies (JAR files), add the following dependencies to pom.xml.
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version></dependency>
Need to modify <version>2.2.4</version>. All code examples in this article use the versions listed above. A copy of the pom.xml file can be found here.
If this library is used for web applications, make sure to keep a copy in the WEB-INF/lib folder. Alternatively, the GSON library can be placed on the application server to provide it to web applications.
Example
Here is a simple example:
public class Person { private String name; private int age; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } @Override public String toString() { return name + ":" +age; }}Entities are very simple, with two fields. Of course, the fields in the entity can also be of List or Set type.
Gson gson = new Gson();List<Person> persons = new ArrayList<Person>(); for (int i = 0; i < 10; i++) { Person p = new Person(); p.setName("name" + i); p.setAge(i * 5); persons.add(p);}String str = gson.toJson(persons);The above code focuses on the Gson object, which provides the toJason() method to convert the object into a Json string. The str object value of the above code is:
[{"name":"name0","age":0},{"name":"name1","age":5},{"name":"name2","age":10},{"name":"name3","age":15},{"name":"name4","age":20},{"name":"name5","age":25},{"name":"name6","age":30},{"name":"name7","age":35},{"name":"name8","age":40},{"name":"name9","age":45}]
Very standard json data, very simple, haha.
Let’s take a look at the deserialization of gson. Gson provides the fromJson() method to implement methods from Json related objects to java entities.
In daily applications, we usually encounter two situations: converting into a single entity object and converting into an object list or other structures.
Let’s look at the first one:
For example, the json string is:
[{"name":"name0","age":0}]Code:
Person person = gson.fromJson(str, Person.class);
Provide two parameters, namely the json string and the type of the object that needs to be converted.
The second type is converted to list type:
Code:
List<Person> ps = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType());for(int i = 0; i < ps.size() ; i++){ Person p = ps.get(i); System.out.println(p.toString());}You can see that the above code uses TypeToken, which is a data type converter provided by gson and can support various data collection type conversions.
There are so many basic uses of Gson. As for annotation, you can refer to gson's official documentation. I hope it will be helpful to students who are beginners in Java and gson.