Java-->Json解析网页数据

--> 官方解析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方便!

************之前忘的Detail类************

  1 package com.dragon.java.hwgsonparse;
  2 
  3 public class Detail {
  4     private int id;
  5     private String city;
  6     private String county;
  7     private String date;
  8     private String day_conditon;
  9     private String day_wind;
 10     private String day_temperature;
 11     private String night_codition;
 12     private String night_wind;
 13     private String night_temperature;
 14     private long update_time;
 15 
 16     public Detail() {
 17         super();
 18     }
 19 
 20     public Detail(int id, String city, String county, String date,
 21             String day_conditon, String day_wind, String day_temperature,
 22             String night_codition, String night_wind, String night_temperature,
 23             long update_time) {
 24         super();
 25         this.id = id;
 26         this.city = city;
 27         this.county = county;
 28         this.date = date;
 29         this.day_conditon = day_conditon;
 30         this.day_wind = day_wind;
 31         this.day_temperature = day_temperature;
 32         this.night_codition = night_codition;
 33         this.night_wind = night_wind;
 34         this.night_temperature = night_temperature;
 35         this.update_time = update_time;
 36     }
 37 
 38     public int getId() {
 39         return id;
 40     }
 41 
 42     public void setId(int id) {
 43         this.id = id;
 44     }
 45 
 46     public String getCity() {
 47         return city;
 48     }
 49 
 50     public void setCity(String city) {
 51         this.city = city;
 52     }
 53 
 54     public String getCounty() {
 55         return county;
 56     }
 57 
 58     public void setCounty(String county) {
 59         this.county = county;
 60     }
 61 
 62     public String getDate() {
 63         return date;
 64     }
 65 
 66     public void setDate(String date) {
 67         this.date = date;
 68     }
 69 
 70     public String getDay_conditon() {
 71         return day_conditon;
 72     }
 73 
 74     public void setDay_conditon(String day_conditon) {
 75         this.day_conditon = day_conditon;
 76     }
 77 
 78     public String getDay_wind() {
 79         return day_wind;
 80     }
 81 
 82     public void setDay_wind(String day_wind) {
 83         this.day_wind = day_wind;
 84     }
 85 
 86     public String getDay_temperature() {
 87         return day_temperature;
 88     }
 89 
 90     public void setDay_temperature(String day_temperature) {
 91         this.day_temperature = day_temperature;
 92     }
 93 
 94     public String getNight_codition() {
 95         return night_codition;
 96     }
 97 
 98     public void setNight_codition(String night_codition) {
 99         this.night_codition = night_codition;
100     }
101 
102     public String getNight_wind() {
103         return night_wind;
104     }
105 
106     public void setNight_wind(String night_wind) {
107         this.night_wind = night_wind;
108     }
109 
110     public String getNight_temperature() {
111         return night_temperature;
112     }
113 
114     public void setNight_temperature(String night_temperature) {
115         this.night_temperature = night_temperature;
116     }
117 
118     public long getUpdate_time() {
119         return update_time;
120     }
121 
122     public void setUpdate_time(long update_time) {
123         this.update_time = update_time;
124     }
125 
126     @Override
127     public String toString() {
128         return "Detail [id=" + id + ", city=" + city + ", county=" + county
129                 + ", date=" + date + ", day_conditon=" + day_conditon
130                 + ", day_wind=" + day_wind + ", day_temperature="
131                 + day_temperature + ", night_codition=" + night_codition
132                 + ", night_wind=" + night_wind + ", night_temperature="
133                 + night_temperature + "]";
134     }
135 
136 }
Detail

 

posted on 2016-08-24 12:28  西门吃雪  阅读(2048)  评论(0编辑  收藏  举报

导航