全部展开
以下工具类型是json和object相互转换的: 不知道能不能帮你/**
*json工具类
*/
packagecom.xlhu.util;
importjava.io.IOException;
导入Java.text.date format;
导入Java.text.simple date format;
importorg.Apache.com mons.lang.string utils;
importorg.Apache.poi.hssf.record.formula.functions.t;
importorg.codehaus.gyd hk.map.object mapper;
importorg.codehaus.gyd hk.map.annotate.JSON serialize.inclusion;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
//*
*普通薯片的简单包装11。
*
* @ 3231愚人节236313431303231331363533 e 58685 e 5a EB 9313愚人节26133 author Calvin
*/
publicclassJsonBinder{
privatestaticloggerlogger=logger factory.getlogger (JSON binder.class );
私有对象映射器;
publicjsonbinder (inclusion inclusion ) {
映射器=new object mapper (;
//设置要包含在输出中的属性
mapper.getSerializationConfig ().setserializationinclusion (inclusion );
将输入设置为忽略存在于JSON字符串中但在Java对象中没有的属性
mapper.getdeserializationconfig (
org.codehaus.gyd hk.map.deserializationconfig.feature.fail _ on _ unknown _ properties,false;
mapper.configure (org.codehaus.gyd hk.JSON parser.feature.allow _ unquoted _ field _ names,true );
}
//*
*创建将所有属性输出到Json字符串的Binder。
*/
publicstaticjsonbinderbuildnormalbinder (
returnewjsonbinder (inclusion.always;
}
//*
创建一个在Json字符串中只输出非空属性的Binder。
*/
publicstaticjsonbinderbuildnonnullbinder (
returnewjsonbinder (inclusion.non _ null;
}
//*
*创建Binder,以仅将初始值已更改的属性输出到Json字符串。
*/
publicstaticjsonbinderbuildnondefaultbinder {
returnewjsonbinder (inclusion.non _ default;
}
//*
如果JSON字符串为Null或' Null '字符串,则返回Null。
如果JSON字符串为“[]”,则返回空集合。
*
读取集合(如List/Map ),如果不是类似List的简单类型,请使用以下语句:
* listbeanlist=binder.get mapper (.read value (liststring,newTypeReference ) );
*/
@suppresswarnings('hiding ) ) ) ) ) ) ) ) )。
publictfromjson (stringjsonstring,Classclazz ) {
if (string utils.isempty (JSON string ) ) )
返回空值;
}
try{
return mapper.read value (JSON字符串,clazz;
}catch(ioexceptione ) {
logger.warn (parsejsonstringerror 3360 ) JSONstring,e );
返回空值;
}
}
//*
*如果对象为空,则返回“空”。
*如果集合为空,则返回“[]”。
*/
publicstringtojson (对象对象) {
try{
return mapper.writevalueasstring (object );
}catch(ioexceptione ) {
logger.warn (写入性jsonstringerror : ) object,e );
返回空值;
}
}
//*
*如果不设置默认打印Timestamp毫秒数,请设置转换日期类型的格式模式。
*/
公共语音数据格式(字符串模式) {
if (string utils.is not blank (pattern ) ) }
date format df=newsimpledateformat (pattern;
mapper.getserializationconfig (.set date format ) ) df;
mapper.getdeserializationconfig (.set date format ) ) df;
}
}
//*
取出Mapper进行进一步设定,或使用其他序列化API。
*/
publicObjectMappergetMapper
返回映射器;
}
}