--> 官方解析jar包: 鏈接:http://pan.baidu.com/s/1pKDnXKv 密碼:694d
--> 離線Json格式檢測工具: 鏈接:http://pan.baidu.com/s/1eSHkrOe 密碼:ju95
--> HttpUtil 工具類
1 package com.dragon.java.jsonwebdata; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 /** 10 * 得到服務器響應的數據流的工具類 11 * 12 * @author Auser 13 * 14 */ 15 public class HttpUtil { 16 17 public static InputStream getInputStreamByPost(String url, String parms) { 18 try { 19 HttpURLConnection conn = (HttpURLConnection) new URL(url) 20 .openConnection(); 21 conn.setReadTimeout(5000); 22 conn.setConnectTimeout(5000); 23 conn.setRequestMethod("POST"); 24 conn.setDoOutput(true); 25 26 OutputStream outputStream = conn.getOutputStream(); 27 outputStream.write(parms.getBytes()); 28 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 29 return conn.getInputStream(); 30 } 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 return null; 35 } 36 }
--> InputStreamUtil 工具類
1 package com.dragon.java.jsonwebdata; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 8 /** 9 * 將服務器響應數據流轉換成String的工具類 10 * 11 * @author Auser 12 * 13 */ 14 public class InputStreamUtil { 15 16 public static String inputStreamToString(InputStream is) { 17 try (BufferedReader br = new BufferedReader(new InputStreamReader(is));) { 18 String line = null; 19 while ((line = br.readLine()) != null) { 20 return line; 21 } 22 } catch (IOException e) { 23 e.printStackTrace(); 24 } 25 return null; 26 } 27 }
--> JsonUtil 工具類
package com.dragon.java.jsonwebdata; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * Json解析的工具類 * * @author Auser * */ public class JsonUtil { /** * 官方解析方法 --> 不管多復雜都可以解析,但是非常復雜!!! * * @param json */ public static void jsonParse(String json) { List<Detail> details = new ArrayList<>(); try { JSONObject object = new JSONObject(json); if (object.getString("status").equals("000000")) { if (object.getString("desc").equals("null")) { details = JSONArray(details, object); for (Detail detail : details) { System.out.println(detail); } } } } catch (JSONException e) { e.printStackTrace(); } } /** * Json數組 * * @param details * @param object * @return List<Detail> * @throws JSONException */ private static List<Detail> JSONArray(List<Detail> details, JSONObject object) throws JSONException { JSONArray array = object.getJSONArray("detail"); for (int i = 0; i < array.length(); i++) { JSONObject object2 = array.getJSONObject(i); int id = object2.getInt("id"); int xhid = object2.getInt("xhid"); String author = object2.getString("author"); String content = object2.getString("content"); String picUrl = object2.getString("picUrl"); String status = object2.getString("status"); Detail detail = new Detail(id, xhid, author, content, picUrl, status); details.add(detail); } return details; } }
--> Test 測試類
package com.dragon.java.jsonwebdata; import java.util.Scanner; /* * Json解析網頁數據 */ public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入頁數:"); int currentPage = scanner.nextInt(); System.out.println("請輸入每頁笑話數:"); int size = scanner.nextInt(); String url = "http://api.1-blog.com/biz/bizserver/xiaohua/list.do"; String parms = "currentPage=" + currentPage + "&size=" + size; // 得到服務器響應的數據並轉換成String String json = InputStreamUtil.inputStreamToString(HttpUtil .getInputStreamByPost(url, parms)); // Json解析 JsonUtil.jsonParse(json); } }
--> 寫完發現用官方的解析方法,那是極復雜的...還是Gson方便!