Json-lib for simple use

Source: Internet
Author: User

Json-lib can convert a Java object to a json string, or convert a Java object to a document in xml format, you can also convert a json string to a Java object or an xml string to a Java object.

1. first go to the official download json-lib Toolkit (http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4)

Json-lib depends on the following packages:

The following is a brief introduction to the use of json-lib:

In actual projects, for code reusability and code redundancy, some methods are often encapsulated and called directly when used. It is not necessary to rewrite the same code every time.

Json-lib can be used to convert a set or a single object to a json string. json-lib of a set or a single object adopts different methods for separate processing:

1) when the parameter is a single object:

// Convert a parameter object to a JSONObject object

JSONObject jsonObject = JSONObject. fromObject (pObject );

// Use the toString () method of JSONObject to obtain the json string

String jsonString = jsonObject. toString ();

2) When the parameter is set type

// Convert a parameter object to a JSONArray object

JSONArray jsonArray = JSONArray. fromObject (pObject );

// Use the toString () method of jsonArray to obtain the json string

String jsonString = jsonArray. toString ();

※Json-lib pay attention to date formatting. If you want to format the output date, you need to manually implement the JsonValueProcessor interface to process the date. The JsonValueProcessor interface declares two methods: processArrayValue (Object value, JsonConfig jsonConfig) and processObjectValue (String key, Object value, JsonConfig jsonConfig), which process the array set and a single Object respectively.

The following is my implementation:

/**

Implement the JsonValueProcessor Interface

*/

Public class DateJsonValueProcessor implements JsonValueProcessor {

Private String format = "yyyy-MM-dd HH: mm: ss ";

Public DateJsonValueProcessor (String format ){

This. format = format;

}

/**

*

Process Arrays

*/

Public Object processArrayValue (Object value, JsonConfig jsonConfig ){

String [] obj = {};

If (value instanceof java. util. Date []) {

SimpleDateFormat sf = new SimpleDateFormat (format );

Java. util. Date [] dates = (java. util. Date []) value;

Obj = new String [dates. length];

For (int I = 0; I <dates. length; I ++ ){

Obj [I] = sf. format (dates [I]);

}

}

If (value instanceof Timestamp []) {

SimpleDateFormat sf = new SimpleDateFormat (format );

Timestamp [] dates = (Timestamp []) value;

Obj = new String [dates. length];

For (int I = 0; I <dates. length; I ++ ){

Obj [I] = sf. format (dates [I]);

}

}

If (value instanceof java. SQL. Date []) {

SimpleDateFormat sf = new SimpleDateFormat (format );

Java. SQL. Date [] dates = (java. SQL. Date []) value;

Obj = new String [dates. length];

For (int I = 0; I <dates. length; I ++ ){

Obj [I] = sf. format (dates [I]);

}

}

Return obj;

}

/**

* Format a single object

*/

Public Object processObjectValue (String key, Object value,

JsonConfig jsonConfig ){

// Judge whether the value is null

If (GSUtils. isEmpty (value )){

Return "";

}

// Determine the value type

If (value instanceof Timestamp ){

String str = new SimpleDateFormat (format). format (Timestamp) value );

Return str;

} Else if (value instanceof java. util. Date ){

String str = new SimpleDateFormat (format). format (java. util. Date) value );

Return str;

} Else if (value instanceof java. SQL. Date ){

String str = new SimpleDateFormat (format). format (java. SQL. Date) value );

Return str;

}

Return value. toString ();

}

Public String getFormat (){

Return format;

}

Public void setFormat (String format ){

This. format = format;

}

}

The JsonValueProcessor interface has not been implemented yet, because json-lib is unknown, so we need to continue. Now we need to use the JsonConfig class by using this class, you can register our custom implementation on json-lib. The key code is as follows:

JsonConfig cfg = new JsonConfig ();

Cfg. registerJsonValueProcessor (java. SQL. Timestamp. class, new DateJsonValueProcessor (pFormatString ));

Cfg. registerJsonValueProcessor (java. util. Date. class, new DateJsonValueProcessor (pFormatString ));

Cfg. registerJsonValueProcessor (java. SQL. Date. class, new DateJsonValueProcessor (pFormatString ));

At this step, we finally have to complete the task, but let's continue. We need to pass the jsonconfig object into fromObject. Note that cfg

1) when the parameter is a single object:

// Convert a parameter object to a JSONObject object

JSONObject jsonObject = JSONObject. fromObject (pObject, cfg );

// Use the toString () method of JSONObject to obtain the json string

String jsonString = jsonObject. toString ();

2) When the parameter is set type

// Convert a parameter object to a JSONArray object

JSONArray jsonArray = JSONArray. fromObject (pObject, cfg );

// Use the toString () method of jsonArray to obtain the json string

String jsonString = jsonArray. toString ();

...

The json-lib function is still very powerful. Here, only work involves this part of knowledge. For more functions, refer to the API. Here we will briefly introduce some common methods for work:

ToBean (String jsonString, Class targetClass) --- converts a json String to a target object. Note that the constructors in the target Class must be empty.

ToString () --- convert a java object to a json string

ToArray () -- get the corresponding java array from the json string

ToArray () -- convert list to Array

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.