One of the Json series beans To json (detailed explanation of the JSONObject class) and jsonjsonobject
Use 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) to json is relatively simple, basically use the following method: method 1 (commonly used) /*** 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 JSONE Xception if the object can not be converted to a proper * JSONObject. */public static JSONObject fromObject (Object object) method 2 (filter, used for Special conversion)/*** 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 not be converted to a prope R * JSONObject. */public static JSONObject fromObject (Object object, JsonConfig jsonConfig) Example code, very complex data type: package json; import java. util. date; import java. util. list; import java. util. map; public class Person {private String name; private Date birthDay; private int age; private String nullStr; private String emptyStr; private boolean isLive; private List <Address> addresses; private Address baseAddress; pri Vate 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 age) {this. age = age;} public boolean isLive () {return isLive;} public void setLive (boolean isLive) {this. isLi Ve = 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 vo Id 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 <Perso N> 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 number p. setBirthDay (new Da Te (System. currentTimeMillis (); // verification date p. setLive (true); // verify the Boolean Type p. setName ("xxx"); // verify the string p. setNullStr (null); // verify null p. setEmptyStr (""); // verify the "" 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 the list set Map attrs = new HashMap (); attrs. put ("key1", "value1"); attrs. put ("key2", "value2"); p.s EtAttrs (attrs); // verify the map set Map sameTest = new HashMap (); sameTest. put ("sameName1", "sameNamevalue1"); sameTest. put ("sameName2", "sameNamevalue2"); p. setSameTest (sameTest); // test returnString = JSONObject with the same name. fromObject (p ). toString ();} these objects and the generated json data will serve the json to bean blog.