Tool classes that convert any format to JSON data format

Source: Internet
Author: User

Java's tool class for converting any format to JSON data format

Package Org.sjw.utils;import Java.beans.introspectionexception;import Java.beans.introspector;import Java.beans.propertydescriptor;import Java.util.list;import Java.util.map;import Java.util.Set;public class        jsonutils {public static String Stringtojson (string s) {if (s = = null) {return Nulltojson ();        } StringBuilder sb = new StringBuilder ();            for (int i = 0; i < s.length (); i++) {Char ch = s.charat (i);                Switch (CH) {case ' "': Sb.append (" \\\ ");            Break                Case ' \ \ ': Sb.append ("\\\\");            Break                Case ' \b ': sb.append ("\\b");            Break                Case ' \f ': Sb.append ("\\f");            Break                Case ' \ n ': sb.append ("\\n");            Break                Case ' \ R ': Sb.append ("\\r");            Break Case ' \ t ': Sb.append ("\\t ");            Break                Case '/': Sb.append ("\\/");            Break Default:if (ch >= ' \u0000 ' && ch <= ' \u001f ') {String SS = Integer.tohex                    String (CH);                    Sb.append ("\\u");                    for (int k = 0; k < 4-ss.length (); k++) {sb.append (' 0 ');                } sb.append (Ss.touppercase ());                } else {sb.append (ch);    }}} return Sb.tostring ();    } public static String Nulltojson () {return "";        public static String Objecttojson (Object obj) {StringBuilder json = new StringBuilder ();        if (obj = = null) {json.append ("\" \ "");        } else if (obj instanceof number) {Json.append (Numbertojson ((number) obj)); } else if (obj instanceof Boolean) {Json.append (Booleantojson (Boolean) OBj));        } else if (obj instanceof String) {json.append ("\" "). Append (Stringtojson (obj.tostring ())). Append (" \ "");        } else if (obj instanceof object[]) {Json.append (Arraytojson ((object[)) obj);        } else if (obj instanceof List) {json.append (Listtojson ((list<?>) obj));        } else if (obj instanceof Map) {json.append (Maptojson (map<?,? >) obj);        } else if (obj instanceof Set) {json.append (Settojson ((set<?>) obj));        } else {json.append (Beantojson (obj));    } return json.tostring ();    } public static String Numbertojson (number number) {return number.tostring ();    } public static String Booleantojson (Boolean bool) {return bool.tostring (); }/** * @param bean * Bean Object * @return String * * public static string Beantojson (object b        EAN) {StringBuilder json = new StringBuilder (); Json.append ("{");        propertydescriptor[] props = null; try {props = Introspector.getbeaninfo (Bean.getclass (), object.class). Getpropertydescriptor        S (); } catch (Introspectionexception e) {} if (props! = null) {for (int i = 0; i < props.length;                    i++) {try {String name = Objecttojson (Props[i].getname ());                    String value = Objecttojson (Props[i].getreadmethod (). Invoke (bean));                    Json.append (name);                    Json.append (":");                    Json.append (value);                Json.append (",");        } catch (Exception e) {}} Json.setcharat (Json.length ()-1, '} ');        } else {json.append ("}");    } return json.tostring (); }/** * @param list * List Object * @return String */public static string Listtojson (list< ?&Gt        List) {StringBuilder json = new StringBuilder ();        Json.append ("["); if (list = null && list.size () > 0) {for (Object obj:list) {json.append (Objectto                Json (obj));            Json.append (",");        } Json.setcharat (Json.length ()-1, '] ');        } else {json.append ("]");    } return json.tostring ();  }/** * @param array * Object Arrays * @return String */public static string Arraytojson (object[]        Array) {StringBuilder json = new StringBuilder ();        Json.append ("["); if (array = null && array.length > 0) {for (Object Obj:array) {json.append (objec                Ttojson (obj));            Json.append (",");        } Json.setcharat (Json.length ()-1, '] ');        } else {json.append ("]");    } return json.tostring ();     }/** * @param map * Map Object* @return String */public static string Maptojson (map<?,? > Map) {StringBuilder json = new Stringbui        Lder ();        Json.append ("{"); if (map! = null && map.size () > 0) {for (Object Key:map.keySet ()) {Json.append (ob                Jecttojson (key));                Json.append (":");                Json.append (Objecttojson (Map.get (key));            Json.append (",");        } Json.setcharat (Json.length ()-1, '} ');        } else {json.append ("}");    } return json.tostring ();  }/** * @param set * Collection Object * @return String */public static string Settojson (set<?>        Set) {StringBuilder json = new StringBuilder ();        Json.append ("["); if (set! = null && set.size () > 0) {for (Object obj:set) {json.append (objecttojso                N (obj));            Json.append (","); } Json.setcharat (Json.lengtH ()-1, '] ');        } else {json.append ("]");    } return json.tostring (); }}


Tool classes that convert any format to JSON data format

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.