Using Json-lib-2.4-jdk15.jar, dependent package 1) commons-lang-2.6.jar2) commons-beanutils-1.8.3.jar3) COMMONS-COLLECTIONS-3.2.1.JAR4) commons- LOGGING-1.1.1.JAR5) Ezmorph-1.0.6.jarbean to JSON is relatively simple, basically use the following methods: Mode one (Common)/** * Creates a jsonobject.<br> * Inspects the object type to call the correct Jsonobject factory method. * Accepts JSON formatted strings, Maps, Dynabeans and JavaBeans. * * @param Object * @throws jsonexception If the object can is converted to a proper * jsonobject. */public static Jsonobject Fromobject (Object object) mode two (filter, special conversion used)/** * Creates a JSONOBJECT.<BR&G T * Inspects the object type to call the correct Jsonobject factory method. * Accepts JSON formatted strings, Maps, Dynabeans and JavaBeans. * * @param Object * @throws jsonexception If the object can is converted to a proper * jsonobject. */public static Jsonobject Fromobject (Object object, Jsonconfig jsonconfig) example code, very complexMiscellaneous data types: Package Json;import java.util.date;import Java.util.list;import Java.util.map;public class Person {private Stri ng name;private Date birthday;private int age;private string Nullstr;private string Emptystr;private boolean Islive;priva Te list<address> addresses;private Address baseaddress;private map attrs;private map Sametest;public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthDay;} public void Setbirthday (Date birthDay) {this.birthday = BirthDay;} public int getage () {return age;} public void Setage (int.) {this.age = age;} public Boolean islive () {return islive;} public void Setlive (Boolean islive) {this.islive = islive;} Public list<address> getaddresses () {return addresses;} public void setaddresses (list<address> addresses) {this.addresses = addresses;} Public Map Getattrs () {return attrs;} public void Setattrs (Map attrs) {this.attrs = Attrs;} Public String Getnullstr () {return nullstr;} Public void Setnullstr (String nullstr) {this.nullstr = Nullstr;} Public String Getemptystr () {return emptyStr;} public void Setemptystr (String emptyStr) {this.emptystr = emptyStr;} Public Map Getsametest () {return sametest;} public void Setsametest (Map sametest) {this.sametest = sametest;} Public Address getbaseaddress () {return baseaddress;} public void setbaseaddress (Address baseaddress) {this.baseaddress = baseaddress;}} Package Json;import Java.util.list;public class Address {private String address;private list<person> sametest; Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} Public list<person> Getsametest () {return sametest;} public void Setsametest (list<person> sametest) {this.sametest = Sametest;}} public static void Main (string[] args) {//An object to JSON String returnstring = null; Person p = new person (); P.setage (1);//Verify the Digital p.setbirthday (new Date (System.currenttimemillis ()));//Validation Date p.setlive (true);//Validation BooleanType p.setname ("xxx");//validation string p.setnullstr (null);//verify NULL P.SETEMPTYSTR ("");//Verify "" String address address = new address (); Address.setaddress ("base Address"); P.setbaseaddress (address);//Basic Bean type list<address> List = new arraylist<address> (); Address Address1 = new address (); Address1.setaddress ("Address1"); Person Sametestperson = new person (); Sametestperson.setname ("Samename"); list<person> samenamelist = new arraylist<person> (); Samenamelist.add (Sametestperson); Address1.setsametest (samenamelist);//Verify the same name Test List.add (ADDRESS1); Address address2 = new address (); Address2.setaddress ("Address2"); List.add (ADDRESS2); P.setaddresses (list);//Verify list set Map attrs = new HashMap (); Attrs.put ("Key1", "value1"); Attrs.put ("Key2", "value2"); P.setattrs (attrs);//verify map set Map sametest = new HashMap (); Sametest.put ("SameName1", "sameNamevalue1"); Sametest.put ("SameName2", "sameNamevalue2"); P.setsametest (sametest);//Verify the same name Test returnstring = Jsonobject.fromobject (p). tOstring ();} These objects and the resulting JSON data are serviced by the JSON to Bean blog post.
One of the JSON series Bean to JSON (Jsonobject Class)