--> Gson解析jar包: 链接:http://pan.baidu.com/s/1slCeq77 密码:f9ig
--> 官方Json解析工具类:
1 package com.dragon.java.jsonwebdata; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.json.JSONArray; 7 import org.json.JSONException; 8 import org.json.JSONObject; 9 10 /** 11 * Json解析的工具类 12 * 13 * @author Auser 14 * 15 */ 16 public class JsonUtil { 17 /** 18 * 官方解析方法 --> 不管多复杂都可以解析,但是非常复杂!!! 19 * 20 * @param json 21 */ 22 public static void jsonParse(String json) { 23 List<Detail> details = new ArrayList<>(); 24 try { 25 JSONObject object = new JSONObject(json); 26 if (object.getString("status").equals("000000")) { 27 if (object.getString("desc").equals("null")) { 28 details = JSONArray(details, object); 29 for (Detail detail : details) { 30 System.out.println(detail); 31 } 32 } 33 } 34 } catch (JSONException e) { 35 e.printStackTrace(); 36 } 37 } 38 39 /** 40 * Json数组 41 * 42 * @param details 43 * @param object 44 * @return List<Detail> 45 * @throws JSONException 46 */ 47 private static List<Detail> JSONArray(List<Detail> details, 48 JSONObject object) throws JSONException { 49 JSONArray array = object.getJSONArray("detail"); 50 for (int i = 0; i < array.length(); i++) { 51 JSONObject object2 = array.getJSONObject(i); 52 int id = object2.getInt("id"); 53 int xhid = object2.getInt("xhid"); 54 String author = object2.getString("author"); 55 String content = object2.getString("content"); 56 String picUrl = object2.getString("picUrl"); 57 String status = object2.getString("status"); 58 Detail detail = new Detail(id, xhid, author, content, picUrl, 59 status); 60 details.add(detail); 61 } 62 return details; 63 } 64 }
--> Gson解析工具类:
--> 需要一个Data类 包括:
private String status;
private String desc;
private List<Detail> detail;
且必须有无参构造方法和属性的getter、setter方法
1 package com.dragon.java.gsonwebdata; 2 3 import java.util.List; 4 5 import com.dragon.java.jsonwebdata.Detail; 6 import com.google.gson.Gson; 7 8 /* 9 * Gson解析的工具类 10 */ 11 public class GsonUtil { 12 13 /** 14 * Gson解析 15 * 16 * @param json 17 */ 18 public static void gsonParse(String json) { 19 Gson gson = new Gson(); 20 Info info = gson.fromJson(json, Info.class); 21 List<Detail> detail = info.getDetail(); 22 if (info.getStatus().equals("000000")) { 23 if (info.getDesc() == null) { 24 for (Detail detail2 : detail) { 25 System.out.println(detail2); 26 } 27 } 28 } 29 } 30 }
--> 有Gson解析谁还用官方的解析方法啊...