Json data conversion interface JsonValueProcessor needs to be implemented when converting the date format. [java] packageorg. rps. util; importjava. text. simpleDateFormat; importjava. util. date; importnet. sf. json. jsonConfig; importnet
Json data conversion interface JsonValueProcessor needs to be implemented when converting the date format.
[Java]
Package org. rps. util;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import net. sf. json. JsonConfig;
Import net. sf. json. processors. JsonValueProcessor;
/***
* Json formatted data Implementation
* @ Author YHC
*/
Public class JsonValueFormat implements JsonValueProcessor {
// Date Format
Private String pattern = "yyyy-MM-dd HH: mm: ss ";
@ Override
Public Object processArrayValue (Object object, JsonConfig config ){
Return null;
}
@ Override
Public Object processObjectValue (String key, Object value, JsonConfig jsonConfig ){
If (value instanceof Date ){
String format = new SimpleDateFormat (pattern). format (value );
Return format;
}
Return null;
}
}
Package org. rps. util;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import net. sf. json. JsonConfig;
Import net. sf. json. processors. JsonValueProcessor;
/***
* Json formatted data Implementation
* @ Author YHC
*/
Public class JsonValueFormat implements JsonValueProcessor {
// Date Format
Private String pattern = "yyyy-MM-dd HH: mm: ss ";
@ Override
Public Object processArrayValue (Object object, JsonConfig config ){
Return null;
}
@ Override
Public Object processObjectValue (String key, Object value, JsonConfig jsonConfig ){
If (value instanceof Date ){
String format = new SimpleDateFormat (pattern). format (value );
Return format;
}
Return null;
}
}
How to use:
1. Create a JsonConfig object.
2. register the Json value converter. The first parameter is the type to be converted, and the second parameter is the conversion processing class.
3. Add the jsonConfig object to the object for json data conversion.
[Java]
JsonConfig config = new JsonConfig ();
Config. registerJsonValueProcessor (Date. class, new JsonValueFormat ());
JSONArray jsonArray = JSONArray. fromObject (lists, config );
JsonConfig config = new JsonConfig ();
Config. registerJsonValueProcessor (Date. class, new JsonValueFormat ());
JSONArray jsonArray = JSONArray. fromObject (lists, config );