使用json-lib-2.4-jdk15.jar,依赖的包 1)commons-lang-2.6.jar 2)commons-beanutils-1.8.3.jar 3)commons-collections-3.2.1.jar 4)commons-logging-1.1.1.jar 5)ezmorph-1.0.6.jar bean 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的博文服务。
时间: 2024-10-29 00:18:26