--> 官方解析jar包: 链接:http://pan.baidu.com/s/1pKDnXKv 密码:694d

--> 离线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-->Json解析网页数据的更多相关文章

  1. 使用java开源工具httpClient及jsoup抓取解析网页数据

    今天做项目的时候遇到这样一个需求,需要在网页上展示今日黄历信息,数据格式如下 公历时间:2016年04月11日 星期一 农历时间:猴年三月初五 天干地支:丙申年 壬辰月 癸亥日 宜:求子 祈福 开光 ...

  2. java抓取网页数据,登录之后抓取数据。

    最近做了一个从网络上抓取数据的一个小程序.主要关于信贷方面,收集的一些黑名单网站,从该网站上抓取到自己系统中. 也找了一些资料,觉得没有一个很好的,全面的例子.因此在这里做个笔记提醒自己. 首先需要一 ...

  3. [java] jsoup 解析网页获取省市区域信息

    到国家统计局抓取数据, 到该class下解析数据 /** * jsoup解析网页 * @author xwolf * @date 2016-12-13 18:11 * @since V1.0.0 */ ...

  4. Java抓取网页数据(原网页+Javascript返回数据)

    有时候由于种种原因,我们需要采集某个网站的数据,但由于不同网站对数据的显示方式略有不同! 本文就用Java给大家演示如何抓取网站的数据:(1)抓取原网页数据:(2)抓取网页Javascript返回的数 ...

  5. Java抓取网页数据(原来的页面+Javascript返回数据)

    转载请注明出处! 原文链接:http://blog.csdn.net/zgyulongfei/article/details/7909006 有时候因为种种原因,我们须要採集某个站点的数据,但因为不同 ...

  6. Jackson:我是最牛掰的 Java JSON 解析器(有点虚)

    在当今的编程世界里,JSON 已经成为将信息从客户端传输到服务器端的首选协议,可以好不夸张的说,XML 就是那个被拍死在沙滩上的前浪. 很不幸的是,JDK 没有 JSON 库,不知道为什么不搞一下.L ...

  7. json解析出来数据为空解决方法

    从APP端或从其他页面post,get过来的数据一般因为数组形式.因为数组形式不易传输,所以一般都会转json后再发送.本以为发送方json_encode(),接收方json_decode(),就解决 ...

  8. 使用JAVA抓取网页数据

    一.使用 HttpClient 抓取网页数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  9. Java正则表达式解析网页源码

    <!DOCTYPE html> <html lang="zh-Hans"> <head> <meta charset="utf- ...

随机推荐

  1. Oracle Form Data Entry Sample

    I shared a data entry example form here in this post for Oracle Forms beginner developers, so that t ...

  2. python 高级特性

    from http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 set set和dict ...

  3. mysql简介

    1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数 ...

  4. unittest框架介绍

    1.test fixture(测试框架) 测试准备前要做的工作和测试执行完成后要做的工作,例如测试前需要把数据初始化,测试完成后需要把测试环境中需要关的东西都关掉.主要包括setUp()和tearDo ...

  5. linux笔记:权限管理-sudo

    sudo可以将只有root可以使用的命令授权给普通用户: 授权的过程实际是修改配置文件: 授权示例: 普通用户使用sudo权限的示例:

  6. Ubuntu 16.04服务器安装及软件配置

    1.配置静态地址 vim /etc/network/interfaces auto enp1s0 iface enp1s0 inet static address 192.168.1.131 netm ...

  7. android 导入自己的生成的jar,老是 could not find class

    最近开始学习android,开发一个小项目,功能很简单,就是从服务器上获取数据,之后显示在手机上.打算把访问服务器的功能打包成一个jar文件.然后android 引入jar包. 在eclipse 里 ...

  8. vue服务端渲染

    这篇文章写得还蛮好https://segmentfault.com/a/1190000006701796 从官方网站下载了例子看,用es6写的,还好之前看过es6不然都看不懂,正好es6的东西一起熟悉 ...

  9. 1057 N的阶乘(大数运算)

    题目链接:51nod 1057 N的阶乘 #include<cstdio> using namespace std; typedef long long ll; ; const int m ...

  10. apache log4j日志工具使用入门[maven 项目配置]

    简单的介绍下Maven项目中有关org.apache.log4j.Logger的使用.[1]首先我们需要找到 org.apache.log4j.Logger的坐标,并配置到POM.xml <de ...