1 //第一種 2 public static void jsonTx() { 3 String json = "{'status':200,'message':'查詢成功','data':[{'id':1,'name':'name1','describe':'第一條數據'}]}"; 4 JSONObject data = JSONObject.fromObject(json); 5 int status = data.optInt("status"); 6 JSONArray data1 = data.getJSONArray("data"); 7 if (status==200) { 8 for(int i=0;i<data1.size();i++){ 9 String s = data1.getString(i); 10 System.out.println(s); 11 JSONObject data2 = JSONObject.fromObject(s); 12 System.out.println(data2.getString("id")); 13 System.out.println(data2.getString("name")); 14 System.out.println(data2.getString("describe")); 15 } 16 }else if(status==500) { 17 String msg = data.getString("message"); 18 System.out.println(msg); 19 } 20 } 21 22 23 24 //第二種 25 public static void getJson() { 26 String json = "{'response':{'data':[{'address':'北京','province':'海淀','district':'西三旗','city':'地鐵'}]},'status':'ok'}"; 27 String json1 = "[{\"TaskID\":3,\"Mobile\":\"13789679619\",\"Status\":5,\"ReportTime\":\"2015-06-24T15:52:54.73\",\"ReportCode\":\"S:終止\"},{\"TaskID\":4,\"Mobile\":\"13789679618\",\"Status\":4,\"ReportTime\":\"2015-06-24T16:32:35.583\",\"ReportCode\":\"成功\"}]"; 28 29 String string = "[{\"depid\":\"5\",\"score\":\"10\"},{\"depid\":\"4\",\"score\":\"40\"},{\"depid\":\"4\",\"score\":\"30\"},{\"depid\":\"5\",\"score\":\"30\"}]"; 30 31 JSONArray jsonArray11 = new JSONArray(); 32 JSONObject jsonObject = JSONObject.fromObject(json); 33 String status = jsonObject.getString("status"); 34 // System.out.println(str); 35 String response = jsonObject.getString("response"); 36 JSONObject jsonObject2 = JSONObject.fromObject(response); 37 JSONArray data = jsonObject2.getJSONArray("data"); 38 for(int i=0;i<data.size();i++){ 39 String s = data.getString(i); 40 JSONObject data2 = JSONObject.fromObject(s); 41 System.out.println(data2.getString("address")); 42 System.out.println(data2.getString("province")); 43 System.out.println(data2.getString("district")); 44 System.out.println(data2.getString("city")); 45 } 46 47 //解析第二個 48 JSONArray jsonObject1 = JSONArray.fromObject(json1); 49 for(int i=0;i<jsonObject1.size();i++){ 50 String s = jsonObject1.getString(i); 51 JSONObject data2 = JSONObject.fromObject(s); 52 System.out.println(data2.getString("TaskID")); 53 System.out.println(data2.getString("Mobile")); 54 System.out.println(data2.getString("Status")); 55 System.out.println(data2.getString("ReportTime")); 56 System.out.println(jsonArray11.size()); 57 } 58 System.out.println("*******************"); 59 //解析第三個 60 JSONArray fromObject = JSONArray.fromObject(string); 61 Map<String,Integer> map = new HashMap<String, Integer>(); 62 63 for (Object object : fromObject) 64 { 65 JSONObject jsonObject3 = (JSONObject) object; 66 String depid = (String)jsonObject3.get("depid"); 67 Integer score = Integer.valueOf((String)jsonObject3.get("score")); 68 if (map.containsKey(depid)) 69 { 70 int integer = map.get(depid); 71 map.put(depid, integer+score); 72 } 73 else 74 { 75 map.put(depid, score); 76 } 77 } 78 Set<Entry<String, Integer>> entrySet = map.entrySet(); 79 JSONArray jsonArray = new JSONArray(); 80 81 for (Entry<String, Integer> entry : entrySet) 82 { 83 JSONObject jsonObject4 = new JSONObject(); 84 jsonObject4.put("depid",entry.getKey()); 85 jsonObject4.put("score",String.valueOf(entry.getValue())); 86 jsonArray.add(jsonObject4); 87 } 88 System.out.println(jsonArray.toString()); 89 }
轉自:https://www.cnblogs.com/QQParadise/articles/5558816.htmlhtml