1.JSON conversion to Java
@Test Public void Jsontojava () { System.out.println ("JSON string to Java code"); = "{\" password\ ": \" \ ", \" username\ ": \" Zhang San \ "}";//Add escape character = Jsonobject.fromobject (jsonstr) ; = Jsonobj.getstring ("username"); = jsonobj.optstring ("password"); System.out.println ("JSON--->java\n username=" + username + "\ t password=" + password); c19/>}
Operation Result:
JSON string to Java code JSON--->java username= Zhang San password=
2.convert Java to JSON
@Test Public void Javatojson () { System.out.println ("Java code encapsulated as JSON string"); New Jsonobject (); Jsonobj.put ("username", "Zhang San"); Jsonobj.put ("password", "" "); System.out.println ("Java--->json \ n" + jsonobj.tostring ()); }
Operation Result:
Java code encapsulated as JSON string Java--->json {"username": "Zhang San", "Password": ""}
3.JSON conversion to XML
@Test Public voidJsontoxml () {System.out.println ("JSON string to XML string"); String Jsonstr= "{\" password\ ": \" \ ", \" username\ ": \" Zhang San \ "}"; Jsonobject JSON=Jsonobject.fromobject (JSONSTR); XMLSerializer XMLSerializer=NewXMLSerializer (); Xmlserializer.setrootname ("User_info"); Xmlserializer.settypehintsenabled (false); String XML=Xmlserializer.write (JSON); System.out.println ("JSON--->xml \ n" +XML); }
Operation Result:
JSON string to XML string JSON---><?xml version= "1.0" encoding= "UTF-8"?><user_info>< Password/><username> Zhang San </username></user_info>
4.XML conversion to JSON
@Test Public void Xmltojson () { System.out.println ("XML string to JSON string"); = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? ><user_info><password></password><username > Zhang San </username></user_info> "; New XMLSerializer (); = Xmlserializer.read (XML); System.out.println ("XML--->json \ n" + json.tostring ()); }
Operation Result:
9:20:18default type Stringxml--->json {"password": [], "username": "Zhang San "}
5.javaBean conversion to JSON
@Test Public void Javabeantojson () { System.out.println ("JavaBean to JSON string"); New UserInfo (); Userinfo.setusername ("Zhang San"); Userinfo.setpassword (""); = Jsonobject.fromobject (userInfo); System.out.println ("JavaBean--->json \ n" + json.tostring ()); }
Operation Result:
JavaBean to JSON string JavaBean--->json {"password": "", "username": "Zhang San"}
6.javaBean to XML
@Test public void Javabeantoxml () {System.out.println (" JavaBean to XML string "
=
new
UserInfo (); Userinfo.setusername ( "Zhang San"
"" = Jsonobject.fromobject (userInfo); XMLSerializer XMLSerializer = new XMLSerializer (); String XML = Xmlserializer.write (JSON, "UTF-8" javabean--->xml \ n "+ XML); }
Operation Result:
JavaBean to XML string JavaBean---><?xml version= "1.0" encoding= "UTF-8"?><o>< Password type= "string"/><username type= "string" > Zhang San </username></o>
Paste all the code:
PackageOrg.xiaowu.json.demo;Importorg.junit.Test;ImportNet.sf.json.JSON;ImportNet.sf.json.JSONObject;ImportNet.sf.json.xml.XMLSerializer; Public classJsonobjectdemo {@Test Public voidJsontojava () {System.out.println ("JSON string to Java code"); String Jsonstr= "{\" password\ ": \" \ ", \" username\ ": \" Zhang San \ "}"; Jsonobject Jsonobj=Jsonobject.fromobject (JSONSTR); String username= Jsonobj.getstring ("username"); String Password= jsonobj.optstring ("Password"); System.out.println ("JSON--->java\n username=" +username+ "\ t password=" +password); } @Test Public voidJavatojson () {System.out.println ("Java code encapsulated as JSON string"); Jsonobject Jsonobj=NewJsonobject (); Jsonobj.put ("Username", "Zhang San"); Jsonobj.put ("Password", "" "); System.out.println ("Java--->json \ n" +jsonobj.tostring ()); } @Test Public voidJsontoxml () {System.out.println ("JSON string to XML string"); String Jsonstr= "{\" password\ ": \" \ ", \" username\ ": \" Zhang San \ "}"; Jsonobject JSON=Jsonobject.fromobject (JSONSTR); XMLSerializer XMLSerializer=NewXMLSerializer (); Xmlserializer.setrootname ("User_info"); Xmlserializer.settypehintsenabled (false); String XML=Xmlserializer.write (JSON); System.out.println ("JSON--->xml \ n" +XML); } @Test Public voidXmltojson () {System.out.println ("XML string to JSON string"); String XML= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? ><user_info><password></password><username > Zhang San </username></user_info> "; XMLSerializer XMLSerializer=NewXMLSerializer (); JSON JSON=xmlserializer.read (XML); System.out.println ("XML--->json \ n" +json.tostring ()); } @Test Public voidJavabeantojson () {System.out.println ("JavaBean to JSON string"); UserInfo UserInfo=NewUserInfo (); Userinfo.setusername ("Zhang San"); Userinfo.setpassword (""); Jsonobject JSON=Jsonobject.fromobject (UserInfo); System.out.println ("JavaBean--->json \ n" +json.tostring ()); } @Test Public voidJavabeantoxml () {System.out.println ("JavaBean to XML string"); UserInfo UserInfo=NewUserInfo (); Userinfo.setusername ("Zhang San"); Userinfo.setpassword (""); Jsonobject JSON=Jsonobject.fromobject (UserInfo); XMLSerializer XMLSerializer=NewXMLSerializer (); String XML= Xmlserializer.write (JSON, "UTF-8"); System.out.println ("JavaBean--->xml \ n" +XML); }}
PackageOrg.xiaowu.json.demo; Public classUserInfo { PublicString username; PublicString password; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; }}
And the jar packages that the project must import, note the version information
Jsonobject about the conversion relationship between Xml,json,bean