[轉] http://www.cnblogs.com/mailingfeng/archive/2012/01/18/2325707.htmlhtml
1. json-lib是一個java類庫,提供將Java對象,包括beans, maps, collections, java arrays and XML等轉換成JSON,或者反向轉換的功能。java
2. json-lib 主頁 : http://json-lib.sourceforge.net/apache
3.執行環境json
須要如下類庫支持數組
4.功能示例this
這裏經過JUnit-Case例子給出代碼示例spa
1 package com.mai.json; 2 3 import static org.junit.Assert.assertEquals; 4 5 import java.util.ArrayList; 6 import java.util.Date; 7 import java.util.HashMap; 8 import java.util.Iterator; 9 import java.util.List; 10 import java.util.Map; 11 import net.sf.ezmorph.Morpher; 12 import net.sf.ezmorph.MorpherRegistry; 13 import net.sf.ezmorph.bean.BeanMorpher; 14 import net.sf.json.JSONArray; 15 import net.sf.json.JSONObject; 16 import net.sf.json.util.JSONUtils; 17 18 import org.apache.commons.beanutils.PropertyUtils; 19 import org.junit.Test; 20 21 public class JsonLibTest { 22 23 /* 24 * 普通類型、List、Collection等都是用JSONArray解析 25 * 26 * Map、自定義類型是用JSONObject解析 27 * 能夠將Map理解成一個對象,裏面的key/value對能夠理解成對象的屬性/屬性值 28 * 即{key1:value1,key2,value2......} 29 * 30 * 1.JSONObject是一個name:values集合,經過它的get(key)方法取得的是key後對應的value部分(字符串) 31 * 經過它的getJSONObject(key)能夠取到一個JSONObject,--> 轉換成map, 32 * 經過它的getJSONArray(key) 能夠取到一個JSONArray , 33 * 34 * 35 */ 36 37 //通常數組轉換成JSON 38 @Test 39 public void testArrayToJSON(){ 40 boolean[] boolArray = new boolean[]{true,false,true}; 41 JSONArray jsonArray = JSONArray.fromObject( boolArray ); 42 System.out.println( jsonArray ); 43 // prints [true,false,true] 44 } 45 46 47 //Collection對象轉換成JSON 48 @Test 49 public void testListToJSON(){ 50 List list = new ArrayList(); 51 list.add( "first" ); 52 list.add( "second" ); 53 JSONArray jsonArray = JSONArray.fromObject( list ); 54 System.out.println( jsonArray ); 55 // prints ["first","second"] 56 } 57 58 59 //字符串json轉換成json, 根據狀況是用JSONArray或JSONObject 60 @Test 61 public void testJsonStrToJSON(){ 62 JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" ); 63 System.out.println( jsonArray ); 64 // prints ["json","is","easy"] 65 } 66 67 68 //Map轉換成json, 是用jsonObject 69 @Test 70 public void testMapToJSON(){ 71 Map map = new HashMap(); 72 map.put( "name", "json" ); 73 map.put( "bool", Boolean.TRUE ); 74 map.put( "int", new Integer(1) ); 75 map.put( "arr", new String[]{"a","b"} ); 76 map.put( "func", "function(i){ return this.arr[i]; }" ); 77 78 JSONObject jsonObject = JSONObject.fromObject( map ); 79 System.out.println( jsonObject ); 80 } 81 82 //複合類型bean轉成成json 83 @Test 84 public void testBeadToJSON(){ 85 MyBean bean = new MyBean(); 86 bean.setId("001"); 87 bean.setName("銀行卡"); 88 bean.setDate(new Date()); 89 90 List cardNum = new ArrayList(); 91 cardNum.add("農行"); 92 cardNum.add("工行"); 93 cardNum.add("建行"); 94 cardNum.add(new Person("test")); 95 96 bean.setCardNum(cardNum); 97 98 JSONObject jsonObject = JSONObject.fromObject(bean); 99 System.out.println(jsonObject); 100 101 } 102 103 //普通類型的json轉換成對象 104 @Test 105 public void testJSONToObject() throws Exception{ 106 String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}"; 107 JSONObject jsonObject = JSONObject.fromObject( json ); 108 System.out.println(jsonObject); 109 Object bean = JSONObject.toBean( jsonObject ); 110 assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) ); 111 assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) ); 112 assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) ); 113 assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) ); 114 assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) ); 115 System.out.println(PropertyUtils.getProperty(bean, "name")); 116 System.out.println(PropertyUtils.getProperty(bean, "bool")); 117 System.out.println(PropertyUtils.getProperty(bean, "int")); 118 System.out.println(PropertyUtils.getProperty(bean, "double")); 119 System.out.println(PropertyUtils.getProperty(bean, "func")); 120 System.out.println(PropertyUtils.getProperty(bean, "array")); 121 122 List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("array")); 123 for(Object object : arrayList){ 124 System.out.println(object); 125 } 126 127 } 128 129 130 //將json解析成複合類型對象, 包含List 131 @Test 132 public void testJSONToBeanHavaList(){ 133 String json = "{list:[{name:'test1'},{name:'test2'}],map:{test1:{name:'test1'},test2:{name:'test2'}}}"; 134 // String json = "{list:[{name:'test1'},{name:'test2'}]}"; 135 Map classMap = new HashMap(); 136 classMap.put("list", Person.class); 137 MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap); 138 System.out.println(diyBean); 139 140 List list = diyBean.getList(); 141 for(Object o : list){ 142 if(o instanceof Person){ 143 Person p = (Person)o; 144 System.out.println(p.getName()); 145 } 146 } 147 } 148 149 150 //將json解析成複合類型對象, 包含Map 151 @Test 152 public void testJSONToBeanHavaMap(){ 153 //把Map當作一個對象 154 String json = "{list:[{name:'test1'},{name:'test2'}],map:{testOne:{name:'test1'},testTwo:{name:'test2'}}}"; 155 Map classMap = new HashMap(); 156 classMap.put("list", Person.class); 157 classMap.put("map", Map.class); 158 //使用暗示,直接將json解析爲指定自定義對象,其中List徹底解析,Map沒有徹底解析 159 MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap); 160 System.out.println(diyBean); 161 162 System.out.println("do the list release"); 163 List<Person> list = diyBean.getList(); 164 for(Person o : list){ 165 Person p = (Person)o; 166 System.out.println(p.getName()); 167 } 168 169 System.out.println("do the map release"); 170 171 //先往註冊器中註冊變換器,須要用到ezmorph包中的類 172 MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry(); 173 Morpher dynaMorpher = new BeanMorpher( Person.class, morpherRegistry); 174 morpherRegistry.registerMorpher( dynaMorpher ); 175 176 177 Map map = diyBean.getMap(); 178 /*這裏的map沒進行類型暗示,故按默認的,裏面存的爲net.sf.ezmorph.bean.MorphDynaBean類型的對象*/ 179 System.out.println(map); 180 /*輸出: 181 {testOne=net.sf.ezmorph.bean.MorphDynaBean@f73c1[ 182 {name=test1} 183 ], testTwo=net.sf.ezmorph.bean.MorphDynaBean@186c6b2[ 184 {name=test2} 185 ]} 186 */ 187 List<Person> output = new ArrayList(); 188 for( Iterator i = map.values().iterator(); i.hasNext(); ){ 189 //使用註冊器對指定DynaBean進行對象變換 190 output.add( (Person)morpherRegistry.morph( Person.class, i.next() ) ); 191 } 192 193 for(Person p : output){ 194 System.out.println(p.getName()); 195 /*輸出: 196 test1 197 test2 198 */ 199 } 200 201 } 202 203 204 205 } 206 .下面提供上面例子所需的資源,包括jar包和代碼 207 /Files/mailingfeng/json-lib/json-lib用例所需jar包和java類.rar
-------------------- 補充 ---------------------.net
[轉] http://zhidao.baidu.com/question/459016631.htmlcode
[注] 一些接口不必定對,參照上文 htm
json格式以下:{"response":{"data":[{"address":"南京市遊樂園","province":"江蘇","district":"玄武區","city":"南京"}]},"status":"ok"}
但願獲得結果是: 江蘇 南京 玄武區 南京市遊樂園
JSONObject dataJson=new JSONObject("你的Json數據「);JSONObject response=dataJson.getJSONObject("response");JSONArray data=response.getJSONArray("data");JSONObject info=data.getJSONObject(0);String province=info.getString("province");String city=info.getString("city");String district=info.getString("district");String address=info.getString("address"); System.out.println(province+city+district+address);