//第一种
public static void jsonTx() {
String json = "{'status':200,'message':'查询成功','data':[{'id':1,'name':'name1','describe':'第一条数据'}]}";
JSONObject data = JSONObject.fromObject(json);
int status = data.optInt("status");
JSONArray data1 = data.getJSONArray("data");
if (status==200) {
for(int i=0;i<data1.size();i++){
String s = data1.getString(i);
System.out.println(s);
JSONObject data2 = JSONObject.fromObject(s);
System.out.println(data2.getString("id"));
System.out.println(data2.getString("name"));
System.out.println(data2.getString("describe"));
}
}else if(status==500) {
String msg = data.getString("message");
System.out.println(msg);
}
}

 

//第二种
public static void getJson() {
String json = "{'response':{'data':[{'address':'北京','province':'海淀','district':'西三旗','city':'地铁'}]},'status':'ok'}";
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\":\"成功\"}]";

String string = "[{\"depid\":\"5\",\"score\":\"10\"},{\"depid\":\"4\",\"score\":\"40\"},{\"depid\":\"4\",\"score\":\"30\"},{\"depid\":\"5\",\"score\":\"30\"}]";

JSONArray jsonArray11 = new JSONArray();
JSONObject jsonObject = JSONObject.fromObject(json);
String status = jsonObject.getString("status");
// System.out.println(str);   
String response = jsonObject.getString("response");
JSONObject jsonObject2 = JSONObject.fromObject(response);
JSONArray data = jsonObject2.getJSONArray("data");
for(int i=0;i<data.size();i++){
String s = data.getString(i);
JSONObject data2 = JSONObject.fromObject(s);
System.out.println(data2.getString("address"));
System.out.println(data2.getString("province"));
System.out.println(data2.getString("district"));
System.out.println(data2.getString("city"));
}

//解析第二个
JSONArray jsonObject1 = JSONArray.fromObject(json1);
for(int i=0;i<jsonObject1.size();i++){
String s = jsonObject1.getString(i);
JSONObject data2 = JSONObject.fromObject(s);
System.out.println(data2.getString("TaskID"));
System.out.println(data2.getString("Mobile"));
System.out.println(data2.getString("Status"));
System.out.println(data2.getString("ReportTime"));
System.out.println(jsonArray11.size());
}
System.out.println("*******************");
//解析第三个
JSONArray fromObject = JSONArray.fromObject(string);
Map<String,Integer> map = new HashMap<String, Integer>();

for (Object object : fromObject)
{
JSONObject jsonObject3 = (JSONObject) object;
String depid = (String)jsonObject3.get("depid");
Integer score = Integer.valueOf((String)jsonObject3.get("score"));
if (map.containsKey(depid))
{
int integer = map.get(depid);
map.put(depid, integer+score);
}
else
{
map.put(depid, score);
}
}
Set<Entry<String, Integer>> entrySet = map.entrySet();
JSONArray jsonArray = new JSONArray();

for (Entry<String, Integer> entry : entrySet)
{
JSONObject jsonObject4 = new JSONObject();
jsonObject4.put("depid",entry.getKey());
jsonObject4.put("score",String.valueOf(entry.getValue()));
jsonArray.add(jsonObject4);
}
System.out.println(jsonArray.toString());
}