Mystringutils (convert object to JSON)

Source: Internet
Author: User
Import Java. beans. introspectionexception; import Java. beans. introspector; import Java. beans. propertydescriptor; import Java. math. bigdecimal; import Java. math. biginteger; import Java. text. simpledateformat; import Java. util. date; import Java. util. list; import Java. util. map; import Java. util. set;/*** 2011-4-22 *** @ author chen_liang */public class mystringutils {/*** @ Param Str * Format: yyyy-mm-dd ** @ return J Ava. util. date */public static date string2date (string Str) throws exception {simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); Return SDF. parse (STR);}/*** convert the date to a string in the format of yyyy-mm-dd ** @ return */public static string data2string (date) {simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); Return SDF. format (date);}/*** @ Param OBJ * any object * @ return string */public static string object2js On (Object OBJ) {stringbuilder JSON = new stringbuilder (); If (OBJ = NULL) {JSON. append ("\"\"");} else if (OBJ instanceof string | OBJ instanceof integer | OBJ instanceof float | OBJ instanceof Boolean | OBJ instanceof short | OBJ instanceof double | OBJ instanceof long | OBJ instanceof integer bigdecimal | OBJ instanceof biginteger | OBJ instanceof byte) {JSON. append ("\""). append (string2json (obj. to String ())). append ("\" ");} else if (OBJ instanceof object []) {JSON. append (array2json (object []) OBJ);} else if (OBJ instanceof list) {JSON. append (list2json (list <?>) OBJ);} else if (OBJ instanceof map) {JSON. append (map2json (Map <?, ?>) OBJ);} else if (OBJ instanceof set) {JSON. append (set2json (set <?>) OBJ);} else {JSON. append (bean2json (OBJ);} return JSON. tostring ();}/*** @ Param bean * bean object * @ return string */public static string bean2json (Object bean) {stringbuilder JSON = new stringbuilder (); JSON. append ("{"); propertydescriptor [] props = NULL; try {props = introspector. getbeaninfo (bean. getclass (), object. class ). getpropertydescriptors ();} catch (introspectionexception e) {e. printstacktrace ();} If (props! = NULL) {for (INT I = 0; I <props. length; I ++) {try {string name = object2json (props [I]. getname (); string value = object2json (props [I]. getreadmethod (). invoke (bean); JSON. append (name); JSON. append (":"); JSON. append (value); JSON. append (",");} catch (exception e) {e. printstacktrace () ;}} JSON. setcharat (JSON. length ()-1, '}');} else {JSON. append ("}");} return JSON. tostring ();} // Private methods ............ .................................... /*** @ Param map * map object * @ return string */Private Static string map2json (Map <?, ?> Map) {stringbuilder JSON = new stringbuilder (); JSON. append ("{"); If (map! = NULL & map. size ()> 0) {for (Object key: map. keyset () {JSON. append (object2json (key); JSON. append (":"); JSON. append (object2json (map. get (key); JSON. append (",");} JSON. setcharat (JSON. length ()-1, '}');} else {JSON. append ("}");} return JSON. tostring ();}/*** @ Param Set * Collection object * @ return string */Private Static string set2json (set <?> Set) {stringbuilder JSON = new stringbuilder (); JSON. append ("["); If (set! = NULL & set. size ()> 0) {for (Object OBJ: Set) {JSON. append (object2json (OBJ); JSON. append (",");} JSON. setcharat (JSON. length ()-1, ']');} else {JSON. append ("]");} return JSON. tostring ();}/*** @ Param S * parameter * @ return string */Private Static string string2json (string s) {If (S = NULL) Return ""; 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> = '\ u000000' & Ch <=' \ u001f') {string Ss = integer. tohexstring (CH); sb. append ("\ U"); For (int K = 0; k <4-ss. le Ngth (); k ++) {sb. append ('0');} sb. append (ss. touppercase ();} else {sb. append (CH) ;}} return sb. tostring ();}/*** @ Param array * object array * @ return string */Private Static string array2json (object [] array) {stringbuilder JSON = new stringbuilder (); JSON. append ("["); If (array! = NULL & array. length> 0) {for (Object OBJ: array) {JSON. append (object2json (OBJ); JSON. append (",");} JSON. setcharat (JSON. length ()-1, ']');} else {JSON. append ("]");} return JSON. tostring ();}/*** @ Param list * list object * @ return string */Private Static string list2json (list <?> List) {stringbuilder JSON = new stringbuilder (); JSON. append ("["); If (list! = NULL & list. size ()> 0) {for (Object OBJ: List) {JSON. append (object2json (OBJ); JSON. append (",");} JSON. setcharat (JSON. length ()-1, ']');} else {JSON. append ("]");} return JSON. tostring ();}}

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.