Orderdto orderdto = Jsonutil.json2object (JSON,
New Typeref<orderdto> () {
});
Package Com.sf.module.bspcommon.util.json;import Java.io.ioexception;import Java.text.simpledateformat;import Java.util.timezone;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Com.fasterxml.jackson.annotation.jsoninclude.include;import Com.fasterxml.jackson.databind.deserializationfeature;import Com.fasterxml.jackson.databind.jsonnode;import Com.fasterxml.jackson.databind.objectmapper;import Com.fasterxml.jackson.databind.serializationfeature;import com.sf.module.bspcommon.util.jsonexception;/** * Description: Jsonutil Tool Class * * <pre> * History * *************************** * ID DATE Person REASON * 1 March 7, 2015 SFI t0250 Create * **************************************************************************** * </pre> * * @a Uthor sfit0250 * @since 1.0 */public class Jsonutil {private static Logger Logger = Loggerfactory.getlogger (jsonutil.class );p rivate static final Objectmapper Objectmapper;/** formatted time string */private static final String date_time_format = "Yyyy-mm-dd HH:mm:ss"; static {objectmapper = new OBJ Ectmapper ();//Remove the default timestamp format objectmapper.configure (serializationfeature.write_dates_as_timestamps, false);// Set to Beijing, China Time zone Objectmapper.settimezone (Timezone.gettimezone ("gmt+8")); Objectmapper.configure ( Serializationfeature.write_null_map_values, false);//null value does not serialize Objectmapper.setserializationinclusion (Include.NON_ NULL);//deserialization, the property does not exist for compatible processing objectmapper.getdeserializationconfig (). Withoutfeatures (deserializationfeature.fail_on _unknown_properties);//serialization, the uniform format of the date Objectmapper.setdateformat (new SimpleDateFormat (Date_time_format)); O Bjectmapper.configure (Serializationfeature.fail_on_empty_beans, false); Objectmapper.configure ( Deserializationfeature.fail_on_unknown_properties, false);//single-Quote processing objectmapper.configure ( Com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES, True);} /** * JSON converted to Object * * @param JSON * @param clazz * @return */public static <T> T Json2objeCT (String json, class<t> clazz) {try {return Objectmapper.readvalue (JSON, clazz);} catch (IOException e) {logger.er Ror ("", e); throw new RuntimeException ("Parse JSON error");}} public static <T> T Json2object (String JSON, typeref<t> tr) {try {return (T) objectmapper.readvalue (JSON, TR.G Ettype ());} catch (IOException e) {logger.error ("", e); throw new RuntimeException ("Parse JSON error", e);}} /** * obj converted to JSON * * @param entity * @return */public static <T> String Object2json (T entity) {try {return OBJECTM Apper.writevalueasstring (entity);} catch (IOException e) {logger.error ("", e); throw new RuntimeException ("Convert JSON Error");}} /** * obj object converted to tree JSON * * @param obj * @return */public static Jsonnode Object2treejson (object obj) {try {return Objectma Pper.valuetotree (obj);} catch (Exception e) {logger.error ("", e); throw new RuntimeException ("Convert JSON Error");}} /** * Decode JSON string into object * * @param <T> * @param json * @param valueType * @return T */@Sup Presswarnings ("Unchecked") public static <T> T decode (String json, class<?> valueType) {try {return ( T) Objectmapper.readvalue (JSON, valueType); } catch (Exception e) {throw new Jsonexception (JSON, E); }}///**//* Object Conversion JSON ignores specified attribute//* @param obj//* @param Pros Ignore attribute name//* @return//*///public static String Object2jsoni Gnoreproperties (Object obj, string...//pros) {//try {//Filterprovider filters = new Simplefilterprovider (). addfilter (o bj//. GetClass (). GetName (), simplebeanpropertyfilter//. Filteroutallexcept (Pros));//Objectmapper.setfilters ( filters);//Objectmapper.setannotationintrospector (new//jacksonannotationintrospector () {//private static final Long serialversionuid = 1l;////public Object findfilterid (annotatedclass ac) {//return ac.getname ();//}//});//Return Objectmapper.writevalueasstring (obj);//} catch (Exception e) {//Log.error (e);//throw new RuntimeException ("Convert JSON error" );// }// }}
Jsonutil object and JSON cross-transfer