Json-lib's study notes

Source: Internet
Author: User

JSON, as I understand it, is a list of strings that can be used to represent an object or to represent an array. It saves a lot of label content than the XML file.

about what JSON is, here we don't introduce too much.

Json-lib is a Java class library (official website: http://json-lib.sourceforge.net/) to achieve the following functions:

1. Convert JavaBeans, maps, collections, Java arrays, and XML into JSON format data:
2. Convert JSON format data into JavaBeans object

Json-lib Required JAR Package:

(1) Commons-beanutils-1.8.3.jar
(2) Commons-collections-3.2.1.jar
(3) Commons-lang-2.6.jar
(4) Commons-logging-1.1.1.jar
(5) Ezmorph-1.0.6.jar
(6) Json-lib-2.4-jdk15.jar

Part I: Converting Java objects to JSON format (string)

1. Handling multiple objects (arrays, collections List, or set)

Convert an object of type "array" to JSON format

Jsonarray.fromobject (passing in an object of type "array");

Description: The array object here can be array, object array, collection class (set set);

2. Handling a single object (Map or JavaBean)

Converts an object of the map type, or a single JavaBean or nested object into JSON format;

Jsonobject.fromobject (an object that passes into a map type, or a single JavaBean or nested object);

3, the use of Jsonconfig properties: Applicable to JavaBean, Map;

Example code:

New jsonconfig (); Config.setexcludes (new//  Specifies which properties are not included in the conversion ) new person ("A001 "," Jack "//  The previous Configuration object System.out.println (jsonobject) is passed in at the time of conversion ;

Part Two: converting data in JSON Format (string) to Java objects

1. Handling Multiple Objects

Converts a JSON-formatted string into an Array:

Jsonarray Jsonarray = Jsonarray.fromobject ("[89,90,99]"= Jsonarray.toarray (jsonarray); SYSTEM.OUT.PRINTLN (array); System.out.println (Arrays.aslist ((object[]) array);

2. Handling a single object

Turn the Json string into JavaBean and Map:

The core approach:
(1) Convert a string in Json form to Map:

Jsonobject jsonobject = jsonobject.fromobject (str); Map<string, object> map = (map<string, object>) Jsonobject.tobean (jsonobject, map.  Class);

(2) Convert a string in Json form to JavaBean:

Jsonobject == (person) jsonobject.tobean (jsonobject, person.  Class);

Note: When converting a Json-formatted string to JavaBean, it is important to note that there must be a parameterless constructor in the JavaBean, otherwise the error of the initialization method cannot be reported.

Resources:

1. Use of Json and Jsonlib-sunzn-Blog Park
Http://www.cnblogs.com/sunzn/archive/2013/02/12/2910241.html

Description: You can use the introductory level of the composition of the tutorial, itself json-lib is not difficult.

2.

Json-lib framework, converting JSON, XML no longer difficult-hoojo-Blog Park
Http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html

3.

Conversion between Java objects and JSON objects-gchb9527 's column-Blog channel-csdn.net
http://blog.csdn.net/gchb9527/article/details/8688279

4.

Json Lib Tutorial-OPEN development Experience Library
Http://www.open-open.com/lib/view/open1326597921734.html

Json-lib's study notes

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.