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