JSON conversion Tool Gson--list to JSON

Source: Internet
Author: User
Tags tojson

Jsona very common way of exchanging data, and because we nowWebFramework uses theEasyui(one of the most commonly usedTableTable-boundJsondata),Jsonapplication is essential, the type of data we now get from the backend database isEntities andList, all belong to the set, the nature is the same, from the entity orListturn intoJsondata to the interface is the most critical.

actually Java in Json There are many kinds of parsing methods that we can use directly Java Jsonobject Library, besides Fastjson,gson,jackson and so on. Json processing libraries, in common, not only facilitate our development, conversion efficiency is also faster.

Today we will first look at the Gson--google honor produced.


first, the introduction Jar Package

If it is a MAVEN project, it can be introduced directly into the POM, but it is also possible to introduce the jar package directly.

<dependency> <groupId>com.google.code.gson</groupId>  <artifactid>gson</ Artifactid>  <version>2.3.1</version></dependency>

Second, use the list to JSON as an example

The use of the very simple, the most important three steps:

Instance a Gsongson Gson = Newgson ();//instance a generic list (typically returned from a database query) list<class> list = new arraylist<class> ();// Call the conversion method ToJson string json = Gson.tojson (list);

Iii. Practical Issues

If you only want to export an entity attribute or have associated other entities in the entity, we do not want to associate the entity (  Listnested in thisList) is also exported toJson, especially the latter one is more common, before we are fromListinRemoveIrrelevant content, but our entity becomes time, beforeRemoveIt is out of control. Look again throughGsonHow to handle this situation.

1. The first is to identify the attributes that need to be converted by annotations on the entity attributes

Publicclass school{@Expose private String code;  @Expose private String name; Private String remark privateset<build> builds = new hashset<build> ();}

2. Using Gsonbuilder

The only class that is different from the one that is instantiated is Gsonbuilder, and the following method needs to be called:

Gson Gson = Newgsonbuilder ()        . Excludefieldswithoutexposeannotation ()//<span style= "Font-family:simsun;" > Export only attributes in entities </span> with @expose annotations        . Create ();  

By default, @expose annotations do not work unless you call the Gsonbuilder.excludefieldswithoutexposeannotation () method when you create Gson with Gsonbuilder.

Note: You can use the Transient keyword to declare a field if you want to include only certain fields that are outside the language.

Gsonbuilder Common settings:

        Gson Gson = new Gsonbuilder ()          . Excludefieldswithoutexposeannotation ()//Do not export attributes with @expose annotations in the entity          . Enablecomplexmapkeyserialization ()//The key supporting map is in the form of a complex object          . Serializenulls (). Setdateformat ("Yyyy-mm-dd HH:mm:ss: SSS ")//time into a specific format            . Setfieldnamingpolicy (fieldnamingpolicy.upper_camel_case)//will capitalize the first letter of the field, note: The @serializedname annotations used on the entity will not take effect.          Setprettyprinting ()//JSON result formatting:          Setversion (1.0)    //There are fields that are not initially available and will be added as a version upgrade, so when serializing and deserializing, the version number is chosen to serialize.                              The @Since (version number) is perfect for this function. Also the field may be deleted as the version is upgraded, then                              //@Until (version number) can also implement this function, the gsonbuilder.setversion (double) method needs to be called.          Create ();  

Iv. Summary

  Select Conversion Properties The problem is solved, if you want to convert the associated list to JSON, then continue with the business test, continue to stand on the shoulders of giants. We certainly have a solution to our current problems, and we are ready to explore.



Attached a

Kun Brother Siang:

Http://blog.csdn.net/lk_blog/article/category/1172246?viewmode=list


Attached two

Google :

Http://google-gson.googlecode.com/svn/tags/gson-2.3.1/docs/javadocs/com/google/gson/Gson.html


Attached three

Performance test:

JSON parsing performance comparison (Gson and Jackson)

There are many online tests, and the general reflection is Jackson better reload performance for large data volumes ( Ten times or more), but none of them are the latest version, and a new version of the test is later.


JSON conversion Tool Gson--list to 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.