The Java-gson of JSON

Source: Internet
Author: User

Gson-google produced by the JSON processing tools, online praise blockbuster, first to learn.

Let's start by listing one of the reviews found online:

Gson is currently the most fully functional JSON parsing artifact, Gson was originally developed by Google in response to Google's internal needs, but has been used by many companies or users since the first edition was released in May 2008. The application of Gson is mainly for the Tojson and Fromjson two conversion functions, no dependency, no exception to the additional jar, can run directly on the JDK .
Instead, the type of the object and its members must be created before using this object transformation to successfully convert the JSON string to the corresponding object. As long as there are get and set methods in the class,Gson can completely convert the JSON of the complex type to the bean or bean to JSON , which is the artifact of JSON parsing.

Some code of Practice

Import Com.google.gson.*;import com.google.gson.reflect.typetoken;import Org.junit.test;import java.util.ArrayList ; Import Java.util.date;import Java.util.list;public class Gsonuse {
/**
* Gson Common scenes
*/
@Test
public void Usegsoncommon () {
Gson Gson = new Gson (); The most basic tool class for parsing JSON
Gsonbuilder Gsonbuilder = new Gsonbuilder (); Gson object Building class, you can Gson objects to do some global settings. Create a Gson object again
Gson = Gsonbuilder.create (); Can cross Gsonbuilder, generate Gson Object

Jsonparser jsonparser = new Jsonparser (); JSON parser
Jsonelement jsonelement = Jsonparser.parse ("{}"); Parses a JSON string into a JSON element using the JSON parser,
Jsonelement can be jsonobject or jsonarray, according to the actual question conversion
Jsonobject jsonobject = new Jsonobject ();
Jsonobject = Jsonelement.getasjsonobject ();
Jsonarray Jsonarray = new Jsonarray ();
Jsonarray = Jsonarray.getasjsonarray ();

User user = new user ();
User.setage (12);
User.setname ("[123]");
JSON, Bean
String Userjson = Gson.tojson (user);
System.out.println (Userjson);
Bean, JSON
User u = Gson.fromjson (Userjson, User.class);

Construct a collection
list<user> users = new arraylist<user> ();
User u_i = null;
for (int i = 0; i < i++) {
U_i = new User ();
U_i.setage (i);
U_i.setname ("name" + i);
Users.add (u_i);
}
String Usersjson = Gson.tojson (users);
System.out.println (Userjson);
Note: The usage of TypeToken **********************
List<user> userlist = Gson.fromjson (Usersjson, New typetoken<list<user>> () {
}.gettype ());
set<user> Userset = Gson.fromjson (usersjson,new typetoken<set<user>> () {}.getType ());
Map<string,user> UserMap = Gson.fromjson (userjson,new typetoken<map<string,user>> () {}.getType ( ));
System.out.println (userlist);
}
/** * Format JSON */@Test public void Formatejson () {String jsonstr = "[{\" id\ ": \" 1\ ", \" name\ ": \" JSON technology \ "},{\" id\ ": \" 2\ ", \" n Ame\ ": \" Java technology \ "}]"; Creates a Gson object that can be formatted to go out of a JSON string Gson Gson = new Gsonbuilder (). setprettyprinting (). Create (); Jsonparser jsonparser = new Jsonparser (); Jsonelement jsonelement = Jsonparser.parse (JSONSTR); String Formatedjson = Gson.tojson (jsonelement); Formats the JSON string output System.out.println (Formatedjson); }/** * JSON property operation: Delete, add, determine if property exists, modify */@Test public void jsonattropt () {String jsonstr = "[{\" id\ ": \" 1\ ", \" name\ ": \" JSON technology \ "},{\" id\ ": \" 2\ ", \" name\ ": \" Java technology \ "}]"; Jsonparser jsonparser = new Jsonparser (); Jsonelement jsonelement = Jsonparser.parse (JSONSTR); Jsonarray Jsonarray = Jsonelement.getasjsonarray (); Jsonobject jobj = jsonarray.get (0). Getasjsonobject (); Determines whether the attribute id boolean hasid = Jobj.has ("id") exists; System.out.println (Hasid); Add attribute Jobj.addproperty ("age", 12); System.out.println (Jobj); Delete attribute Jobj.remove ("age"); System.out.println (Jobj); Modify the properties, delete them first, and then add Jobj.Remove ("name"); Jobj.addproperty ("name", New Gson (). ToJson ("[8888]")); System.out.println (Jobj); }/** * Some configuration of Gson objects through Gsonbuilder, such as date formatting and so on */@Test public void Gsonbuilderuse () {Gsonbuilder Gsonbuilder = new Gsonbuild ER (); Set the time format Gsonbuilder.setdateformat ("Yyyy-mm-dd hh:mm:ss"); Cancels the escape of the HTML//gsonbuilder.disablehtmlescaping (); Gson Gson = Gsonbuilder.create (); System.out.println (Gson.tojson (New Date ())); System.out.println (Gson.tojson ("<! DOCTYPE html> 

  

The Java-gson of JSON

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.