Java JSON Transformation Library Gson Basic Usage Method Example _java

Source: Internet
Author: User

Download and deploy Gson
Gson GitHub page address: Https://github.com/google/gson
Before working with the Gson API, you need to download the library (jar file) and include it in the classpath. libraries, together with source code and Java documents, can be downloaded from the http://code.google.com/p/google-gson/downloads/list. When the download is complete, add Gson-<version>.jar to the classpath. For readers 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 the code examples in this article use the versions listed above. Pom.xml file copies can be found here.

If this library is for Web applications, make sure that you keep a copy of the Web-inf/lib folder. Alternatively, the Gson library can be placed on the application server for Web applications.

Example

The following is a simple example:

public class Person {

  private String name;
  private int age;

  /**
   * @return the name
   *
  /Public String GetName () {return
    name;
  }

  /**
   * @param name ' name to set
   *
  /public void SetName (String name) {
    this.name = name;
  }

  /**
   * @return The Age
  /public int getage () {return age
    ;
  }

  /** * @param age "age" to
   set
   *
  /public void setage (int age) {
    this.age = age;
  }
  
  @Override public
  String toString ()
  {return
    name + ': ' +age
  }
}

The entity is very simple, two fields, of course, the field in the entity can also be a list or set type.

Gson Gson = new Gson ();
list<person> persons = new arraylist<person> ();
for (int i = 0; i < 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 to a JSON string, with the Str object value of the above code:

[{"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, hehe.

Here's a look at Gson deserialization, Gson provides a Fromjson () method for implementing a method from JSON-related objects to Java entities.

In everyday applications, we typically encounter two situations, turning into a single entity object and converting it into a list of objects or other structures.

First of all, see:

For example, the JSON string is:

[{' name ': ' NAME0 ', ' age ': 0}]

Code:

Person person = Gson.fromjson (str, person.class);

provides two parameters, namely, the JSON string and the type of object that needs to be converted.

Second, convert 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.

The basic use of Gson is so much, as for the annotation aspect can refer to Gson Official document, hope to be able to beginner Java and Gson classmate Help.

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.