javabean:Address
?
package com.baebae.model; public class Address { private String city; private String district; private String street; private int no; getter/setter()... }
?
?
JAVA--JSON互相转换:
?
【1】JAVA——>JSON
?
/** * <1> JAVA-->JSON * 单个对象Object-->JSONObject * 集合List/Array/Set----->JSONArray */ @Test public void testObj2JSON() throws InterruptedException { // 1.bean---->json Address addr = new Address(); addr.setCity("北京"); addr.setDistrict("清河"); addr.setStreet("安宁庄东路"); addr.setNo(200); JSONObject jsonObj = JSONObject.fromObject(addr); System.err.println("____________jsonObj_______________"); System.out.println(jsonObj); // 1.集合-->JSONArray Set<Address> set = new HashSet<Address>(); Address[] addrArray = new Address[100]; List<Address> list = new ArrayList<Address>(); for(int i=0;i<10;i++){ Address address = new Address(); address.setCity("北京"); address.setDistrict("清河"); address.setStreet("安宁庄东路"+i+"号"); address.setNo(200+i); list.add(address); addrArray[i]=address; set.add(address); } // 这里不能用JSONObject.fromObject(collection) JSONArray jsonList = JSONArray.fromObject(list); JSONArray jsonArray = JSONArray.fromObject(addrArray); JSONArray jsonSet = JSONArray.fromObject(set); // 打印结果 Thread.sleep(3000); System.err.println("____________jsonList_______________"); System.out.println(jsonList); Thread.sleep(3000); System.err.println("_____________jsonArray______________"); System.out.println(jsonArray); Thread.sleep(3000); System.err.println("______________jsonSet_____________"); System.out.println(jsonSet); Thread.sleep(3000); }
打印结果:
?
?
____________jsonObj_______________ {"city":"北京","district":"清河","no":200,"street":"安宁庄东路"} ____________jsonList_______________ [{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"}] _____________jsonArray______________ [{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北京","district":"清河","no":201,"street":"安宁庄东路1号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":203,"street":"安宁庄东路3号"},{"city":"北京","district":"清河","no":204,"street":"安宁庄东路4号"},{"city":"北京","district":"清河","no":205,"street":"安宁庄东路5号"},{"city":"北京","district":"清河","no":206,"street":"安宁庄东路6号"},{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":208,"street":"安宁庄东路8号"},{"city":"北京","district":"清河","no":209,"street":"安宁庄东路9号"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null] ______________jsonSet_____________ [{"city":"北京","district":"清河","no":207,"street":"安宁庄东路7号"},{"city":"北京","district":"清河","no":202,"street":"安宁庄东路2号"},{"city":"北京","district":"清河","no":200,"street":"安宁庄东路0号"},{"city":"北