Java-->Json解析网页数据
2016-08-24 12:28
337 查看
--> 官方解析jar包: 链接:http://pan.baidu.com/s/1pKDnXKv 密码:694d
--> 离线Json格式检测工具: 链接:http://pan.baidu.com/s/1eSHkrOe 密码:ju95
--> HttpUtil 工具类
--> InputStreamUtil 工具类
--> JsonUtil 工具类
--> Test 测试类
--> 写完发现用官方的解析方法,那是极复杂的...还是Gson方便!
************之前忘的Detail类************
Detail
--> 离线Json格式检测工具: 链接:http://pan.baidu.com/s/1eSHkrOe 密码:ju95
--> HttpUtil 工具类
package com.dragon.java.jsonwebdata; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * 得到服务器响应的数据流的工具类 * * @author Auser * */ public class HttpUtil { public static InputStream getInputStreamByPost(String url, String parms) { try { HttpURLConnection conn = (HttpURLConnection) new URL(url) .openConnection(); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream outputStream = conn.getOutputStream(); outputStream.write(parms.getBytes()); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { return conn.getInputStream(); } } catch (IOException e) { e.printStackTrace(); } return null; } }
--> InputStreamUtil 工具类
package com.dragon.java.jsonwebdata; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * 将服务器响应数据流转换成String的工具类 * * @author Auser * */ public class InputStreamUtil { public static String inputStreamToString(InputStream is) { try (BufferedReader br = new BufferedReader(new InputStreamReader(is));) { String line = null; while ((line = br.readLine()) != null) { return line; } } catch (IOException e) { e.printStackTrace(); } return null; } }
--> 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方便!
************之前忘的Detail类************
package com.dragon.java.hwgsonparse; public class Detail { private int id; private String city; private String county; private String date; private String day_conditon; private String day_wind; private String day_temperature; private String night_codition; private String night_wind; private String night_temperature; private long update_time; public Detail() { super(); } public Detail(int id, String city, String county, String date, String day_conditon, String day_wind, String day_temperature, String night_codition, String night_wind, String night_temperature, long update_time) { super(); this.id = id; this.city = city; this.county = county; this.date = date; this.day_conditon = day_conditon; this.day_wind = day_wind; this.day_temperature = day_temperature; this.night_codition = night_codition; this.night_wind = night_wind; this.night_temperature = night_temperature; this.update_time = update_time; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCounty() { return county; } public void setCounty(String county) { this.county = county; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getDay_conditon() { return day_conditon; } public void setDay_conditon(String day_conditon) { this.day_conditon = day_conditon; } public String getDay_wind() { return day_wind; } public void setDay_wind(String day_wind) { this.day_wind = day_wind; } public String getDay_temperature() { return day_temperature; } public void setDay_temperature(String day_temperature) { this.day_temperature = day_temperature; } public String getNight_codition() { return night_codition; } public void setNight_codition(String night_codition) { this.night_codition = night_codition; } public String getNight_wind() { return night_wind; } public void setNight_wind(String night_wind) { this.night_wind = night_wind; } public String getNight_temperature() { return night_temperature; } public void setNight_temperature(String night_temperature) { this.night_temperature = night_temperature; } public long getUpdate_time() { return update_time; } public void setUpdate_time(long update_time) { this.update_time = update_time; } @Override public String toString() { return "Detail [id=" + id + ", city=" + city + ", county=" + county + ", date=" + date + ", day_conditon=" + day_conditon + ", day_wind=" + day_wind + ", day_temperature=" + day_temperature + ", night_codition=" + night_codition + ", night_wind=" + night_wind + ", night_temperature=" + night_temperature + "]"; } }
Detail
相关文章推荐
- Java解析txt文件中json数据到List<entity>,并存入数据库
- zTree联想终极篇-->将存储树形数据表封装成自定义TreeNode-->将自定义TreeNode解析成JSON树形字串
- <WP7>解析json数据(初用json.Net)
- Java解析(读取)Json数据
- Java 解析Json数据
- 【Java】json数据解析
- Java解析JSON数据
- java 解析Json格式数据
- java利用url解析网页内容并模拟手动form提交数据
- Java XML JSON 数据解析
- 关于Java解析json格式数据
- Json数据格式解析示例 (语言:Java 包:org.json.jar)
- 很好用的Google 开源项目Gson在java中解析和创建json格式的数据
- java中对JSON格式数据进行解析和映射
- 爬取带ajax的网页信息,需要将java script里的内容解析成json的格式
- java 解析 json格式数据。。。
- 13-6-13 android中Java URL类的实现和JSON数据解析
- java如何解析json字符串(List<Map<String,Object>成功的类型)