Java JSON processing tool class Usage Details, json tool class
This example shares the code of the JSON processing tool class for your reference. The details are as follows:
Import java. io. IOException; import java. util. date; import java. util. hashMap; import java. util. map; import javax. servlet. http. httpServletResponse; import com. alibaba. fastjson. JSON; import com. alibaba. fastjson. serializer. serializerFeature; /***** @ author humf **/public class FastJsonUtil {/*** convert the object into a json String * @ param Object * @ return */public static String toJSONString (object Object) {// DisableCircularReferenceDetect to disable loop reference detection return JSON. toJSONString (object, SerializerFeature. disableCircularReferenceDetect);} // output json public static void write_json (HttpServletResponse response, String jsonString) {response. setContentType ("application/json; UTF-8"); response. setCharacterEncoding ("UTF-8"); try {response. getWriter (). print (jsonString);} catch (IOException e) {e. printStackTrace () ;}/ *** json String called back after ajax submission * @ return */public static String ajaxResult (boolean success, String message) {Map map = new HashMap (); map. put ("success", success); // whether map is successful. put ("message", message); // text message String json = JSON. toJSONString (map); return json ;} /*** automatically prefix the JSON String * @ param json original json String * @ param prefix * @ return the String with the prefix */public static String JsonFormatterAddPrefix (String json, string prefix, Map <String, Object> newmap) {if (newmap = null) {newmap = new HashMap ();} Map <String, Object> map = (Map) JSON. parse (json); for (String key: map. keySet () {Object object = map. get (key); if (isEntity (object) {String jsonString = JSON. toJSONString (object); JsonFormatterAddPrefix (jsonString, prefix + key + ". ", newmap);} else {newmap. put (prefix + key, object) ;}} return JSON. toJSONString (newmap);}/*** determines whether an object is an entity * @ param Object * @ return */private static boolean isEntity (object) {if (Object instanceof String) {return false;} if (object instanceof Integer) {return false;} if (object instanceof Long) {return false;} if (object instanceof java. math. bigDecimal) {return false;} if (object instanceof Date) {return false;} if (object instanceof java. util. collection) {return false;} return true ;}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.