點擊下載json工具java
點擊下載支持jar包json
1.從Object到String
要先用Object對象構造一個JSONObject或者JSONArray對象,而後調用它的toString()方法便可數組
(1)示例一app
Book book=new Book(); book.setName("Java"); book.setPrice(52.3f); JSONObject object=JSONObject.fromObject(book); System.out.println(object.toString());
(2)示例二工具
Book book=new Book(); book.setName("Java"); book.setPrice(52.3f); Book book2=new Book(); book2.setName("C"); book2.setPrice(42.3f); List list=new ArrayList(); list.add(book); list.add(book2); JSONArray arry=JSONArray.fromObject(list); System.out.println(arry.toString()); //結果以下: [{"name":"Java","price":52.3},{"name":"C","price":42.3}]
2.從String到Object
要先用String對象構造一個JSONObject或者JSONArray對象
(1)示例一 ui
String json="{name:'Java',price:52.3}"; JSONObject object=JSONObject.fromObject(json); System.out.println(object.get("name")+" "+object.get("price"));
(2)示例二this
String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]"; JSONArray array=JSONArray.fromObject(json); for(int i=0;i<array.size();i++){ Map o=(Map)array.get(i); System.out.println(o.get("name")+" "+o.get("price")); }
3.從String到Beanspa
(1)單個Bean對象code
String json="{name:'Java',price:52.3}"; JSONObject object=JSONObject.fromObject(json); Product product=(Product)JSONObject.toBean(object,Product.class); System.out.println(product.getName()+" "+product.getPrice());
(2).Bean的數組對象
String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]"; JSONArray array=JSONArray.fromObject(json); Product[] products=(Product[]) JSONArray.toArray(array,Product.class); for(Product p:products){ System.out.println(p.getName()+" "+p.getPrice()); }
使用List和Map集合結合上面方法拼裝自定義json數據
/** * 審批狀態列表 * @return json * @throws Exception */ public String applyList() throws Exception { List<Map<String,String>> list = new ArrayList<Map<String,String>>(); Map<String,String> map = new HashMap<String, String>(); map.put("value", ""); map.put("text", "所有"); list.add(map); map = new HashMap<String, String>(); map.put("value", "-1"); map.put("text", "未經過"); list.add(map); map = new HashMap<String, String>(); map.put("value", "0"); map.put("text", "主任審批中"); list.add(map); map = new HashMap<String, String>(); map.put("value", "1"); map.put("text", "經理審批中"); list.add(map); map = new HashMap<String, String>(); map.put("value", "2"); map.put("text", "已經過"); list.add(map); JSONArray arry=JSONArray.fromObject(list); this.jsonObject = arry.toString(); return JSON; }
獲得以下JSON數據:
[ { "text": "所有", "value": "" }, { "text": "未經過", "value": "-1" }, { "text": "主任審批中", "value": "0" }, { "text": "經理審批中", "value": "1" }, { "text": "已經過", "value": "2" } ]
自定義封裝JSON操做的類
package com.util; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonHelper { //從普通的Bean轉換爲字符串 public static String getJson(Object o){ JSONObject jo=JSONObject.fromObject(o); return jo.toString(); } //從Java的列表轉換爲字符串 public static String getJson(List list){ JSONArray ja=JSONArray.fromObject(list); return ja.toString(); } //從Java對象數組轉換爲字符串 public static String getJson(Object[] arry){ JSONArray ja=JSONArray.fromObject(arry); return ja.toString(); } //從json格式的字符串轉換爲Map對象 public static Map getObject(String s){ return JSONObject.fromObject(s); } //從json格式的字符串轉換爲List數組 public static List getArray(String s){ return JSONArray.fromObject(s); } //從json格式的字符串轉換爲某個Bean public static Object getObject(String s,Class cls){ JSONObject jo=JSONObject.fromObject(s); return JSONObject.toBean(jo, cls); } //從json格式的字符串轉換爲某類對象的數組 public static Object getArray(String s,Class cls){ JSONArray ja=JSONArray.fromObject(s); return JSONArray.toArray(ja, cls); } }
拼json方法:
/** * 統計頁面 * @return * @throws Exception */ public String getMonthFlow() throws Exception{ List list = new ArrayList(); list = totalQuantityManager.getMonthFlow(); ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); ArrayList list3 = new ArrayList(); ArrayList list4 = new ArrayList(); for (int i = 0; i < list.size(); i++) { Object[] strs = (Object[]) list.get(i); list1.add(strs[1]); list2.add(strs[2]); list3.add(strs[4]); list4.add(strs[5]); } Hashtable dic = new Hashtable(); dic.put("huaXue",list1); dic.put("anDan",list2); dic.put("erYangHualiu",list3); dic.put("danYangHuawu",list4); this.jsonObject=JSONObject.fromObject(dic).toString(); return JSON; }
json數據:
{ "huaXue": [ 400, 1132, 601, 500, 120, 90, 1000 ], "anDan": [ 1320, 7000, 601, 234, 120, 50000, 20 ], "erYangHualiu": [ 1320, 1132, 601, 234, 120, 90, 20 ], "danYangHuawu": [ 1320, 1132, 601, 234, 120, 90, 20 ] }