A JSON tool class that provides a conversion between JSON and objects.
The source code is as follows: (Click to download- jsonutils.java , gson-2.2.4.jar )
1 ImportJava.lang.reflect.Type;2 ImportJava.util.Map;3 ImportCom.google.gson.Gson;4 ImportCom.google.gson.GsonBuilder;5 6 /**7 * JSON Tool class8 * 9 */Ten@SuppressWarnings ("Unchecked") One Public classJsonutils { A - Private StaticGson Gson; - the Privatejsonutils () { - } - - Static { +Gsonbuilder GB =NewGsonbuilder (); -Gb.setdateformat ("Yyyy-mm-dd HH:mm:ss"); +Gson =gb.create (); A } at - Public Static FinalString ToJson (Object obj) { - returnGson.tojson (obj); - } - - Public Static Final<T> T Fromjson (FinalString JSON, class<t>clazz) { in returnGson.fromjson (JSON, clazz); - } to + Public Static Final<T> T Fromjson (FinalString json, Type t) { - returnGson.fromjson (JSON, t); the } * $ Public Static FinalMap<string, Object> Fromjson (FinalString JSON) {Panax Notoginseng returnFromjson (JSON, Map.class); - } the +}
JSON Tool Class-Jsonutils.java