/** * */Package Net.nyist.jsondemo.servlet;import java.util.arraylist;import java.util.hashmap;import java.util.List; Import Java.util.map;import org.json.JSONObject;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 9:24:29*/ Public classJsondemo { Public Static voidMain (string[] args) {Map<string, object> map =NewHashmap<string, object>(); List<map<string, object>> list =NewArraylist<map<string, object>>(); Map.put (" Age", A); Map.put ("name","Yuchao"); List.add (map); Map=NewHashmap<string, object>(); Map.put (" Age", +); Map.put ("name","Zhangsan"); List.add (map); Map=NewHashmap<string, object>(); Map.put ("Users", list); Jsonobject Jsonobject=Newjsonobject (map); System. out. println (Jsonobject.tostring ()); }}
/** * */Package Net.nyist.jsondemo.entity;import java.io.Serializable;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 11:53:32*/@SuppressWarnings ("Serial") Public classAddress implements serializable{PrivateString Codenumber; PrivateString City; PublicString Getcodenumber () {returnCodenumber; } Public voidSetcodenumber (String codenumber) { This. Codenumber =Codenumber; } PublicString getcity () {returnCity ; } Public voidsetcity (String city) { This. City =City ; } }/** * */Package Net.nyist.jsondemo.entity;import java.io.Serializable;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 11:54:43*/@SuppressWarnings ("Serial") Public classUser implements serializable{Private intID; PrivateString name; Private intAge ; Privateaddress address; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicAddress getaddress () {returnaddress; } Public voidsetaddress (address address) { This. Address =address; }}/** * */Package Net.nyist.jsondemo.servlet;import java.util.arraylist;import java.util.hashmap;import java.util.List; Import Java.util.map;import Net.nyist.jsondemo.entity.address;import Net.nyist.jsondemo.entity.user;import Org.json.JSONArray;/** * @author Yuchao * * @school the application and development of the mobile device of Nanyang Institute of Technology software 11 soft Mobile Four Classes * * @time 2014-9-17 pm 11:53:03*/ Public classJsonobjectdemo { Public Static voidMain (string[] args) {address address=NewAddress (); Address.setcity ("Henan Nanyang"); Address.setcodenumber ("473000"); User User=NewUser (); User.setid (1); User.setname ("Yuchao"); User.setaddress (address); Map<string, object> map =NewHashmap<string, object>(); List<User> list =NewArraylist<user>(); List.add (user); Address=NewAddress (); Address.setcity ("Zhengzhou, Henan"); Address.setcodenumber ("473000"); User=NewUser (); User.setid (2); User.setname ("liling"); User.setaddress (address); List.add (user); Map.put ("Users", list); Jsonarray Jsonarray=NewJsonarray (); Jsonarray.put (map); System. out. println (Jsonarray.tostring (3)); } }
Using Java code to complete the conversion to JSON format