Java --> parse webpage data in Json format, java -- parse webpage in json format
--> Official parsing jar package: Link: http://pan.baidu.com/s/1pKDnXKv password: 694d
--> Offline Json format detection tool: Link: http://pan.baidu.com/s/1eSHkrOe password: ju95
--> HttpUtil tool class
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 * tool class 11*12 * @ author Auser13 * 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 tool class
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 * converts the server response data stream to a String tool class 10*11 * @ author Auser12 * 13 */14 public class InputStreamUtil {15 16 public static String inputStreamToString (InputStream is) {17 try (BufferedReader br = new BufferedReader (new InputStreamReade R (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 tool class
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-parsed tool class ** @ author Auser **/public class JsonUtil {/*** official Parsing Method --> resolution can be performed regardless of the complexity, but it is very complicated !!! ** @ 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 array ** @ param details * @ param object * @ return List <Detail> * @ throws JSONException */private static List <Detail> JSONArray (List <Detail> details, JSONObject) 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 class
Package com. dragon. java. jsonwebdata; import java. util. parse;/** Json parse webpage data */public class Test {public static void main (String [] args) {parse response = new response (System. in); System. out. println ("Enter the number of pages:"); int currentPage = bytes. nextInt (); System. out. println ("Enter the number of jokes on each page:"); int size = success. nextInt (); String url = "http://api.1-blog.com/biz/bizserver/xiaohua/list.do"; String parms = "currentPage =" + currentPage + "& size =" + size; // obtain the server response data and convert it to String json = InputStreamUtil. inputStreamToString (HttpUtil. getInputStreamByPost (url, parms); // Json parses JsonUtil. jsonParse (json );}}
--> After writing the findings, we use the official parsing method, which is extremely complicated... or convenient for Gson!