Parsing json using fastjson, jackson, json-lib in Java-------------------mom doesn't have to worry about JSON parsing anymore.

Source: Internet
Author: User
Tags goto

1, Fastjson
Introduction Package
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>

JSON string goto Jsonobject/jsonarray
Jsonobject Jsonobject = Jsonobject.parseobject (jsonstring);
Get child nodes
Jsonobject. getString ();
Jsonobject. GetArray ();
Jsonobject. getDate ();
.....
Switch to JavaBean
Jsonobject.tojavaobject (jsonobject,javabean.class);
JSON string direct to JavaBean
Jsonobject.parseobject (jsonstring,javabean.class);
JavaBean to JSON string
String jsonstring = jsonobject.tojsonstring (javaBean)

2. Jackson

The package introduced
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>

JSON string Loaded JavaBean
Objectmapper = new Objectmapper ();
JavaBean javabean= objectmapper.readvalue (jsonstring, javabean.class);

JavaBean to JSON string
Objectmapper = new Objectmapper ();
WriteObject can convert Java objects, eg:javabean/map/list/array, etc.
Jsongenerator.writeobject (bean);
WriteValue has the same function as WriteObject
Objectmapper.writevalue (system.out, bean);

Get child nodes
Objectmapper mapper = new Objectmapper ();
Read json, the entire JSON as the root node
Jsonnode node = Mapper.readtree (jsonstring);
Node.get ();

1.//if The property does not have a value, then JSON is processed, the int type is 0,string type is null, the array is [], setting this attribute can ignore the null Value property
2. Mapper.setserializationinclusion (JsonInclude.Include.NON_EMPTY);
3.//let json be indented for better readability and generally used in the testing and development phases.
4. Mapper.configure (serializationfeature.indent_output, true);
5.//make map key in natural order
6. Mapper.configure (serializationfeature.order_map_entries_by_keys, true);
7.//date output format
8. SimpleDateFormat OutputFormat = new SimpleDateFormat ("yyyy-mm-dd");
9. Mapper.setdateformat (outputformat);

Create a node, generate a JSON string
1.//create a jsonnodefactory,false that provides all nodes without creating Decimalnode
2. Jsonnodefactory factory = new Jsonnodefactory (false);
3.
4.//create jsonfactory and output to console
5 using the streaming Api. Jsonfactory jsonfactory = new Jsonfactory ();
6. Jsongenerator Generator = Jsonfactory.creategenerator (system.out);
7. Objectmapper mapper = new Objectmapper ();
8.
9.//create nodes and data, a Objectnode represents a node object
10. Objectnode Node1 = Factory.objectnode ();
One. Objectnode node2 = Factory.objectnode ();
Node1.put ("a", "a");
Node1.put ("b", "b");
Node2.set ("C", node1);
.
16.//root node
17. Objectnode root = Factory.objectnode ();
Root.put ("root", "root");
Root.set ("children", node2);
Mapper.writetree (generator, root);
21.//output {"root": "root", "children": {"C": {"a": "a", "b": "b"}}}

Other arrays, lists, map references
Http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html
http://blog.csdn.net/gjb724332682/article/details/51586701

3, Json-lib
The packages used
<!--json-lib-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>

JSON string goto Jsonobject/jsonarray
Jsonobject Jsonobject = Jsonobject.fromobject (jsonstring/javabean);
Get child nodes
Jsonobject.getstring ("success")
Switch to JavaBean
Jsonobject.tobean (jsonobject,javabean.class);
JavaBean to JSON string
String jsonstring=jsonobject.tostring ();

List Map Reference
Http://www.cnblogs.com/teach/p/5791029.html

Parsing json using fastjson, jackson, json-lib in Java-------------------mom doesn't have to worry about JSON parsing anymore.

Related Article

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.