Java JSON processing tool class Usage Details, json tool class

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.