Google Gson Use Summary

Source: Internet
Author: User
Tags tojson

Recently began to contact the use of Google Gson Library, personally feel that it is very useful, here a brief introduction to the development of Gson common functions.

1.Serialize JSON

Package Com.ricky.java.gson;
Import java.util.ArrayList;
Import java.util.List;
Import Com.google.gson.Gson;
Import Com.google.gson.GsonBuilder;

Import Com.ricky.java.gson.bean.User; /** * Serialize and Deserialize JSON Data in Java * @author Ricky * */public class Gsonserilize {public STA
		
		tic void Main (string[] args) {serializeobject ();
	Serializelist ();
		public static void SerializeObject () {User user = new user ();
User.setid (1001);
		User.setname ("Ricky");
		
		User.setage (25);
		
		Gson Gson = new Gsonbuilder (). setprettyprinting (). Serializenulls (). Create ();
	System.out.println (Gson.tojson (user));
		
		public static void Serializelist () {list<user> List = new arraylist<user> ();
		User user = new user ();
		User.setid (1001);
		User.setname ("Ricky");
		
		User.setage (25);
		
		List.add (user);
		User User2 = new user ();
		User2.setid (1002);
		User2.setname ("Tim");
		
		User2.setage (24);
		
		List.add (User2); Gson Gson = new Gsonbuilder (). setprettyprinting (). Serializenulls (). Create ();
	System.out.println (Gson.tojson (list));
 }
}


Here the main use of Gson.tojson (Object src)

You can easily serialize Java objects, such as beans, lists, map objects, and so on.


2.Deserialize JSON

Package Com.ricky.java.gson;
Import Java.lang.reflect.Type;
Import java.util.ArrayList;

Import java.util.List;
Import Com.google.gson.Gson;
Import Com.google.gson.GsonBuilder;
Import Com.google.gson.reflect.TypeToken;

Import Com.ricky.java.gson.bean.User; /** * Serialize and Deserialize JSON Data in Java * @author Ricky * */public class Gsondeserilize {public S
		
		tatic void Main (string[] args) {deserializeobject ();
	Deserializelist ();
		
		public static void Deserializeobject () {String json = "{\ id\": 10010,\ "name\": \ "Ricky feng\", \ "age\": 25} ";
		
		Gson Gson = new Gsonbuilder (). setprettyprinting (). Serializenulls (). Create ();
		User user = Gson.fromjson (JSON, user.class);
	SYSTEM.OUT.PRINTLN (user); public static void Deserializelist () {String json = ' [{\ id\ ': 10010,\ ' name\ ': \ ' Ricky feng\ ', \ ' age\ ': 25},{\ ' id\ ':
		
		10011,\ "name\": \ "mike\", \ "age\": 24}] ";
		
		Gson Gson = new Gsonbuilder (). setprettyprinting (). Serializenulls (). Create (); Type ListType = new Typetoken<arraylist<user>> () {}.gettype ();
		list<user> list = Gson.fromjson (JSON, ListType);
	SYSTEM.OUT.PRINTLN (list);
 }
}


This is mainly used for Gson.fromjson (String json, class< user> Classoft)

It is very convenient to deserialize the JSON string into a Java object.


3.Parse JSON

Package Com.ricky.java.gson;
Import Com.google.gson.Gson;
Import Com.google.gson.GsonBuilder;
Import com.google.gson.JsonElement;
Import Com.google.gson.JsonObject;

Import Com.google.gson.JsonParser; /** * How to Parse JSON Data into Java with Gson * @author Ricky * */public class Gsontest {public static void main (
		
		String[] args {String json = "{\ Success\": true,\ "message\": \ "Invalid access Token.\"} ";
		1.jsonparser.parse (String JSON);
		Jsonparser jsonparser = new Jsonparser ();
		Jsonobject Jo = (jsonobject) jsonparser.parse (JSON);
		Boolean success = Jo.get ("Success"). Getasboolean ();
		
		SYSTEM.OUT.PRINTLN (Success);
		String msg = jo.get ("message"). Getasstring ();
		
		System.out.println ("msg=" +msg);
		
		System.out.println ("------------------");
		2.gson.fromjson (String json, class<jsonelement> Classoft);
		Gson Gson = new Gsonbuilder (). setprettyprinting (). Serializenulls (). Create ();
		Jsonelement Jelem = Gson.fromjson (JSON, jsonelement.class); Jsonobject Jobj = Jelem.getasjsonobject ();
		
		Jelem.getasjsonarray ();
		msg = Jobj.get ("message"). Getasstring ();
		
		System.out.println ("msg=" +msg);
		Boolean has = Jobj.has ("Status");
	System.out.println ("has=" +has);
 }

}




There are so many Gson APIs that you are currently exposed to, and more in-depth understanding of Gson will continue to be updated.






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.