標籤:
使用json-lib-2.4-jdk15.jar,依賴的包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相對是比較簡單的,基本都採用以下辦法: 方式一(常用) /** * 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 proper * JSONObject. */ public static JSONObject fromObject( Object object ) 方式二(過濾,特殊轉換時使用) /** * 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 proper * JSONObject. */ public static JSONObject fromObject( Object object, JsonConfig jsonConfig ) 例子代碼,很複雜的資料類型: 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;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 age) {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) { //一 object to json String returnString = null; Person p = new Person(); p.setAge(1);//驗證數字 p.setBirthDay(new Date(System.currentTimeMillis()));//驗證日期 p.setLive(true);//驗證布爾類型 p.setName("xxx");//驗證字串 p.setNullStr(null);//驗證null p.setEmptyStr("");//驗證“”字串 Address address = new Address(); address.setAddress("base address"); p.setBaseAddress(address);//基本的bean類型 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);//驗證相同名字測試 list.add(address1); Address address2 = new Address(); address2.setAddress("address2"); list.add(address2); p.setAddresses(list);//驗證list集合 Map attrs = new HashMap(); attrs.put("key1", "value1"); attrs.put("key2", "value2"); p.setAttrs(attrs);//驗證map集合 Map sameTest = new HashMap(); sameTest.put("sameName1", "sameNamevalue1"); sameTest.put("sameName2", "sameNamevalue2"); p.setSameTest(sameTest);//驗證相同名字測試 returnString = JSONObject.fromObject(p).toString();}這些對象及產生的json資料會為 json to bean的博文服務。
Json系列之一 bean To json(JSONObject類詳解)