Companies are not allowed to introduce Json-lib-2.x.jar so I had to write a basic type of object to JSON class, and then support 8 basic types and Javabean,list,map,array, using the Putjson method, the key value is casually written, value is the object to be converted.
Jsonobjectconvert.java:
PackageCom.pz.util;ImportJava.lang.reflect.Field;Importjava.util.Date;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry; Public classJsonobjectconvert {PrivateStringBuffer SF =NULL; PublicString Simpleobjecttojson (Object o) {SF=NewStringBuffer ("{"); //Sf.append (O.getclass (). Getsimplename ());Field field[] =O.getclass (). Getdeclaredfields (); String content= ""; for(Field F:field) {Try{f.setaccessible (true); Content+ = "\" + f.getname () + "\": \ "" + Convertnull (F.get (o)) + "\", "; } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(illegalaccessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} sf.append (content); returnSf.tostring (). substring (0, Sf.length ()-1) + "}"; } PublicObject Convertnull (Object o) {if(O = =NULL){ return""; }Else { returno; }} @SuppressWarnings ("Unchecked") PublicObject Convertjson (Object o) {if(O = =NULL){ return""; }Else if(OinstanceofBoolean) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofCharacter) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofShort ) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofInteger) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofLong) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofFloat) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofDouble) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofByte) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofList) { returnListtojson ((List) o); }Else if(OinstanceofMap) { returnMaptojson ((MAP) o); }Else if(O.getclass (). IsArray ()) {returnArraytojson (o); }Else if(OinstanceofString) { return"\" "+o.tostring () +" \ ""; }Else if(OinstanceofDate) { return"\" "+o.tostring () +" \ ""; } returnSimpleobjecttojson (o); } @SuppressWarnings ("Unchecked") PublicString Listtojson (List list) {SF=NewStringBuffer ("["); for(inti = 0;i < List.size (); i++) {sf.append (Convertjson (List.get (i) )+ ","); } returnSf.tostring (). substring (0,sf.tostring (). Length ()-1) + "]"; } @SuppressWarnings ("Unchecked") PublicString Maptojson (map map) {SF=NewStringBuffer ("["); Iterator ite=Map.entryset (). iterator (); while(Ite.hasnext ()) {Entry o=(Entry) ite.next (); String Key=Convertjson (O.getkey ()). ToString (); Sf.append ("{\" "+ key.substring (1, Key.length ()-1) +" \ ":"); Sf.append (Convertjson (O.getvalue ())+"},"); } returnSf.tostring (). substring (0,sf.tostring (). Length ()-1) + "]"; } PublicString Arraytojson (Object o) {SF=NewStringBuffer ("["); object[] obj=(object[]) o; for(inti = 0;i < obj.length;i++) {sf.append (Convertjson (obj[i))+ ","); } returnSf.tostring (). substring (0, sf.tostring (). Length ()-1) + "]"; } //The sum method convert to JSON key --value Public Staticstring Putjson (string Key,object value) {Jsonobjectconvert joc=NewJsonobjectconvert (); String jsonstring= "{\" "+ key +" \ ":" + joc.convertjson (value) + "}"; returnjsonstring; }}
Package com.pz.util;
Import Java.lang.reflect.field;import java.util.date;import Java.util.iterator;import Java.util.List;import Java.util.map;import Java.util.Map.Entry;
public class Jsonobjectconvert {private StringBuffer SF = null;public string simpleobjecttojson (Object o) {SF = new S Tringbuffer ("{");//sf.append (O.getclass (). Getsimplename ()); Field field[] = O.getclass (). Getdeclaredfields (); String content = ""; for (Field F:field) {try {f.setaccessible (true); content + = "\" + f.getname () + "\": \ "" + Convertnull (F.get (O)) + "\", ";} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch ( Illegalaccessexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Sf.append (content); return sf.tostring (). substring (0, Sf.length ()-1) + "}"; public Object Convertnull (object o) {if (o = = null) {return "";} else {return o;}} @SuppressWarnings ("unchecked") public object Convertjson (Object o) {if (o = = null) {return "";} else if (o instanceof Boolean) {return "\" "+o.tostring () +" \ "";} else if (o instanceof Character) {return "\" "+o.tostring () +" \ "";} else if (o instanceof short) {return "\" "+o.tostring () +" \ "";} ElSe if (o instanceof Integer) {return "\" "+o.tostring () +" \ "";} else if (o instanceof Long) {return "\" "+o.tostring () +" \ "";} else if (o instanceof Float) {return "\" "+o.tostring () +" \ "";} else if (o instanceof Double) {return "\" "+o.tostring () +" \ "";} else if (o instanceof Byte) {return "\" "+o.tostring () +" \ "";} else if (o instanceof List) {return Listtojson (list) o);} else if (o instanceof Map) {return Maptojson (map) o);} else if (O.getclass (). IsArray ()) {return Arraytojson (o);} else if (o instanceof String) {return "\" "+o.tostring () +" \ "";} else if (o instanceof Date) {return "\" "+o.tostring () +" \ "";} return Simpleobjecttojson (o);} @SuppressWarnings ("unchecked") Public string Listtojson (List list) {SF = new StringBuffer ("["); for (int i = 0;i < L Ist.size (); i++) {Sf.append (Convertjson (List.get (i)) + ",");} Return sf.tostring (). substring (0,sf.tostring (). Length ()-1) + "]";} @SuppressWarnings ("unchecked") public string Maptojson (map map) {SF = new StringBuffer ("["); Iterator ite = Map.entryset ().Iterator (); while (Ite.hasnext ()) {Entry o = (Entry) ite.next (); String key = Convertjson (O.getkey ()). ToString (); Sf.append ("{\" "+ key.substring (1, Key.length ()-1) +" \ ":"); Sf.append ( Convertjson (O.getvalue ()) + "},"); Return sf.tostring (). substring (0,sf.tostring (). Length ()-1) + "]";} Public String Arraytojson (Object o) {sf = new StringBuffer ("["); object[] obj = (object[]) o;for (int i = 0;i < obj.length; i++) {sf.append (Convertjson (obj[i]) + ",");} Return sf.tostring (). substring (0, sf.tostring (). Length ()-1) + "]";} The sum method convert to JSON key--valuepublic static string Putjson (String key,object value) {Jsonobjectconvert J OC = new Jsonobjectconvert (); String jsonstring = "{\" "+ key +" \ ":" + joc.convertjson (value) + "}"; return jsonstring;}}
Conversion classes for common Java type to JSON