Time when json-lib is encapsulated in the background

Source: Internet
Author: User

1. Json-lib can easily convert a Java object into a json string, use the tutorial (http://json-lib.sourceforge.net/usage.html)
However, when Java objects have time types. Json-lib converts the time field:
{"Date": 17, "day": 3, "hours": 0, "minutes": 0, "month": 11, "nanos": 0, "seconds": 0, "time": 345830400000, "timezoneOffset":-480, "year": 80}, "ENAME": "SMITH "}
What should we do?
First (processing in extjs): http://blog.csdn.net/without0815/article/details/7788582
Second (main ):
[Java]
List list = iEmpDao. getListEmp (pageIndex, pageNo );

// Map <String, Object> map = (Map <String, Object>) list. get (0 );
// JSONArray ja1 = new JSONArray (). fromObject (list );
JSONArray ja = new JSONArray ();
JsonConfig jf = new JsonConfig ();
// Jf. registerJsonValueProcessor (java. SQL. Timestamp. class, new DateJsonValueProcessor ("yyyy-MM-dd HH: mm: ss "));
Jf. registerJsonValueProcessor (java. SQL. Timestamp. class, new DateJsonValueProcessor ("yyyy-MM-dd "));
Jf. registerJsonValueProcessor (java. util. Date. class, new DateJsonValueProcessor ("yyyy-MM-dd "));
System. out. println (ja. fromObject (list, jf). toString ());

Return ja. fromObject (list, jf). toString ();
DateJsonValueProcessor implements a JsonValueProcessor interface.
[Java] view plaincopy
Package org. ymm. util;
 
Import java. text. SimpleDateFormat;
Import net. sf. json. JsonConfig;
Import net. sf. json. processors. JsonValueProcessor;
 
Public class DateJsonValueProcessor implements JsonValueProcessor
{

Private String format;
Public DateJsonValueProcessor (String format ){
This. format = format;
}

Public Object processArrayValue (Object value, JsonConfig jsonConfig)
{
Return null;
}
 
Public Object processObjectValue (String key, Object value, JsonConfig jsonConfig)
{
If (value = null)
{
Return "";
}
If (value instanceof java. SQL. Timestamp)
{
String str = new SimpleDateFormat (format). format (java. SQL. Timestamp) value );
Return str;
}
If (value instanceof java. util. Date) www.2cto.com
{
String str = new SimpleDateFormat (format). format (java. util. Date) value );
Return str;
}

Return value. toString ();
}
}
As long as the time type is processed and formatted
Author: without0815

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.