摘要:相互转换的自封装处理对象和工具类将对象变成字符串和对象。
自封装的jackson工具类,处理对象和json之间的转换。
Public class JsonUtil {/** *将对象转换为json字符串* @ param obj * @ return */public static string convertjsonstring(object obj){ object mapper object mapper = new object mapper();String str = nulltry { str = object mapper . writevalueasstring(obj);} catch(Exception e){ e . printstacktrace();}返回str}/* * *将json变成对象* @ param JSON * @ paramobjclass * @ param * @ return */public static t convert obj(String JSON,Class objClass){ object mapper object mapper = new object mapper();T t = nulltry { t = object mapper . read value(JSON,obj class);} catch(Exception e){ e . printstacktrace();} return t;}}
评论前必须登录!
注册