轉載網址:http://blog.sina.com.cn/s/blog_821025b70100wh6v.htmlhtml
上一篇文章中有這麼一句,是後臺的封裝數據。java
1. List集合轉換成json代碼json
List list = new ArrayList();數組
list.add( "first" );服務器
list.add( "second" );this
JSONArray jsonArray2 = JSONArray.fromObject( list );
spa
2. Map集合轉換成json代碼htm
Map map = new HashMap();blog
map.put("name", "json");ip
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
3. Bean轉換成json代碼
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 數組轉換成json代碼
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 通常數據轉換成json代碼
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );