This article uses the Json-lib jar package to convert the JSON to the bean.
1. Convert a string to JSON
Use the Jsonobject.fromobject (str) method to convert a string to a JSON object
Use Jsonobject.put ("attribute", "value") to add properties to the JSON
If you need to convert to a JSON array, simply use the method provided by the Jsonarray object to
/*** Some simple conversions*/ Public Static voidtransformstringtest () {String str= "{" + "\" id\ ":" + "\" 1\ "," + "\" name\ ":" + "\" zhangsan\ "" + "}"; //1. Convert a string to JSONJsonobject jsonobj =jsonobject.fromobject (str); System.out.println (Jsonobj.tostring ()); //JSON Add PropertyJsonobj.put ("Age", "22"); System.out.println (Jsonobj.tostring ()); //2. Converting an object to an arrayJsonarray Jsonarr =Jsonarray.fromobject (jsonobj); System.out.println (Jsonarr.tostring ()); //3. Adding an array to a JSON objectJsonobject obj =NewJsonobject (); Obj.put ("Employees", Jsonarr); System.out.println (Obj.tostring ()); }
/* Output Content * {"id": "1", "name": "Zhangsan"} * {"id": "1", "name": "Zhangsan", "age": "\"} * [{"id": "1", "name" : "Zhangsan", "Age": "{"}] * {"Employees": [{"id": "1", "name": "Zhangsan", "Age": ""}]} * /
2. Convert an object to JSON
First create the People class
Public classpeople {PrivateString name; PrivateString Idcard; Publicpeople () {} Publicpeople (string name, String idcard) { This. Name =name; This. Idcard =Idcard; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getidcard () {returnIdcard; } Public voidSetidcard (String idcard) { This. Idcard =Idcard; } }
Converting an object to JSON also uses the Sonobject.fromobject (obj) method
If it is a list, you need to use Jsonarray to convert the object to a JSON array when you convert to JSON
Public Static voidTransformobjecttest () {people P1=NewPeople ("A", "111111"); //1. Convert an object to JSONSystem.out.println (Jsonobject.fromobject (p1)); List<People> peoplelist =NewArraylist<people>(); Peoplelist.add (p1); //2. Convert list to JSON (need to use array Jsonarray)Jsonarray arr =Jsonarray.fromobject (peoplelist); System.out.println (Arr.tostring ()); } /*Output content * {"Idcard": "111111", "name": "A"} * [{"Idcard": "111111", "name": "A"}]*/
3.JSON Switch to Bean
The JSON-to-bean approach is also very simple, just use the Jsonobject.tobean () method, which you need to pass in the Bean's class
/** * Convert json to bean@param json @param type @return * /publicstatic <T> Object Transformjsontobean (String JSON , class<t> type) { = jsonobject.fromobject (JSON); return Jsonobject.tobean (Jsonobject, type); }
4.JSON Conversion to list<bean> collection
Because it is a collection, you need to use Jsonarray,jsonarray to provide the Tocollection method, which also needs to pass in the Bean's class
Public static <T> Object transformjsontobeanlist (String jsonarr, Class<T> type) { = jsonarray.fromobject (jsonarr); return jsonarray.tocollection (Jsonarray, type); }
/*** Project name: Tools * Project Package Name: Com.songfayuantools.json * Created: July 31, 2017 11:58:51 * created by: administrator-Song Fayuen * created location: */ PackageCom.songfayuantools.json; ImportCom.songfayuantools.entity.UserInfo; ImportNet.sf.json.JSON; ImportNet.sf.json.JSONObject; ImportNet.sf.json.xml.XMLSerializer; /*** Description: Jsonobject How to use the method * Jsonobject-lib package is a Beans,collections,maps,java arrays and XML and JSON to convert each other package. * @authorSongfayuan * July 31, 2017 morning 11:58:51*/ Public classJson {/*** Description: JSON string to Java code *@authorSongfayuan * August 2, 2017 pm 2:24:47*/ Public Static voidJsontojava () {System.out.println ("JSON string to Java code"); String Jsonstr= "{\" password\ ": \" 123456\ ", \" username\ ": \" Zhang San \ "}"; Jsonobject Jsonobject=Jsonobject.fromobject (JSONSTR); String username= Jsonobject.getstring ("username"); String Password= jsonobject.getstring ("Password"); System.err.println ("JSON--->java \ username=" +username+ "\ t passwor=" +password); } /*** Description: Java code encapsulated as JSON string *@authorSongfayuan * August 2, 2017 pm 2:30:58*/ Public Static voidJavatojson () {System.out.println ("Java code encapsulated as JSON string"); Jsonobject Jsonobject=NewJsonobject (); Jsonobject.put ("username", "Song Fayuen"); Jsonobject.put ("Age", 24); Jsonobject.put ("Sex", "male"); System.out.println ("Java--->json \ n" +jsonobject.tostring ()); } /*** Description: JSON string to XML string *@authorSongfayuan * August 2, 2017 pm 2:56:30*/ Public Static voidJsontoxml () {System.out.println ("JSON string to XML string"); String Jsonstr= "{\" username\ ": \" Song Fayuen \ ", \" password\ ": \" 123456\ ", \" age\ ": \" 24\ "}"; Jsonobject Jsonobject=Jsonobject.fromobject (JSONSTR); XMLSerializer XMLSerializer=NewXMLSerializer (); Xmlserializer.setrootname ("User_info"); Xmlserializer.settypehintsenabled (false); String XML=Xmlserializer.write (Jsonobject); System.out.println ("JSON--->xml \ n" +XML); } /*** Description: XML string to JSON String *@authorSongfayuan * August 2, 2017 pm 3:19:25*/ Public Static voidXmltojson () {System.out.println ("XML string to JSON string"); String XML= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?><user_info><password>123456</password>< username> Song Fayuen </username></user_info> "; XMLSerializer XMLSerializer=NewXMLSerializer (); JSON JSON=xmlserializer.read (XML); System.out.println ("XML--->json \ n" +json.tostring ()); } /*** Description: JavaBean to JSON String *@authorSongfayuan * August 2, 2017 pm 3:39:10*/ Public Static voidJavabeantojson () {System.out.println ("JavaBean to JSON string"); UserInfo UserInfo=NewUserInfo (); Userinfo.setusername ("Song Fayuen"); Userinfo.setpassword ("123456"); Jsonobject Jsonobject=Jsonobject.fromobject (UserInfo); System.out.println ("Javabean-->json \ n" +jsonobject.tostring ()); } /*** Description: JavaBean to XML String *@authorSongfayuan * August 2, 2017 pm 3:48:08*/ Public Static voidJavabeantoxml () {System.out.println ("JavaBean to XML string"); UserInfo UserInfo=NewUserInfo (); Userinfo.setusername ("Songfayuan"); Userinfo.setpassword ("66666"); Jsonobject Jsonobject=Jsonobject.fromobject (UserInfo); XMLSerializer XMLSerializer=NewXMLSerializer (); String XML= Xmlserializer.write (Jsonobject, "UTF-8"); System.out.println ("JavaBean--->xml \ n" +XML); } Public Static voidMain (String args[]) {//Jsontojava (); //Javatojson (); //Jsontoxml (); //Xmltojson (); //Javabeantojson (); Javabeantoxml (); } }
JSON-to-bean conversion