之前都是自己写后台,自己的server提供数据给client。

近期在看第三方的数据接口,訪问其它站点提供的信息。比方。我们可能自己收集的数据相当有限。可是网上提供了非常多关于天气预报、新闻、星座运势、身份证号、车辆违章、健康医疗、快递查询、ip查询、翻译等的api接口。基本返回数据为类型json和xml

我就喜欢简单便捷的东西。在这解析一下第三方新闻的接口返回的json数据;

我喜欢用谷歌提供的Gson,感觉比JSON去解析要简单。方便,快捷;当然了阿里巴巴提供的fastjson也是极好的。在这仅仅用gson解析了(废话似乎多了点)。

①先看一下我要解析的第三方的数据:(图片看不清能够拖动图片到新的页面标签中看哦~)

②然后从上面能够得到json数据的key值。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

③由于Gson是基于对象的,所以我们要将这些 “键值”建立一个实体类

首先能够看到最外层是三个键值 error_code ,reason,result 然后result中为一个json数组,那么我们将json数组中的数据单独抽出一个对象来。

演示样例代码例如以下:

package com.zml.pojo;

import java.util.List;

/**
* @author 郑明亮
* @Time:2016-3-18 上午10:28:55
* @version 1.0
*/
public class NewsJson {
String error_code;
String reason;
List<News> result; public NewsJson(String error_code, String reason, List<News> result) {
super();
this.error_code = error_code;
this.reason = reason;
this.result = result;
} public NewsJson() {
super();
// TODO Auto-generated constructor stub
} public String getError_code() {
return error_code;
} public void setError_code(String error_code) {
this.error_code = error_code;
} public String getReason() {
return reason;
} public void setReason(String reason) {
this.reason = reason;
} public List<News> getResult() {
return result;
} public void setResult(List<News> result) {
this.result = result;
} @Override
public String toString() {
return "NewsJson [error_code=" + error_code + ", reason=" + reason
+ ", result=" + result + "]";
} }
package com.zml.pojo;
/**
* @author 郑明亮
* @Time:2016-3-18 上午10:27:13
* @version 1.0
*/
public class News {
String ctime;
String title;
String picUrl;
String url; public News(String ctime, String tittle, String picUtl, String url) {
super();
this.ctime = ctime;
this.title = tittle;
this.picUrl = picUtl;
this.url = url;
} public News() {
super();
// TODO Auto-generated constructor stub
} public String getCtime() {
return ctime;
} public void setCtime(String ctime) {
this.ctime = ctime;
} public String getTittle() {
return title;
} public void setTittle(String tittle) {
this.title = tittle;
} public String getPicUtl() {
return picUrl;
} public void setPicUtl(String picUtl) {
this.picUrl = picUtl;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} @Override
public String toString() {
return "News [ctime=" + ctime + ", tittle=" + title + ", picUtl="
+ picUrl + ", url=" + url + "]";
}
}

④然后就是进行解析了,解析方法我在之前的博文中已经介绍了。假设没看的能够,先看看;

传送门 Andorid之Gson解析Json数据

在这直接将方法列出来了:

	public static <T> T getObjectData(String jsonString, Class<T> type) {

		T t = null;
try {
Gson gson = new Gson();
t = gson.fromJson(jsonString, type);
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return t;
}
/**
* 将输入流转换为byte[]
*
* @param is
* @return
*/
public static byte[] IsToByte(InputStream is) { ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int len = 0;
try {
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
try {
bos.flush();
bos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } return bos.toByteArray();
}

⑤測试解析方法:

package com.zml.pojo.test;

import static org.junit.Assert.*;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import org.junit.Test; import com.zml.pojo.NewsJson;
import com.zml.utils.GsonTools; /**
* @author 郑明亮
* @Time:2016年3月18日 上午10:35:39
* @version 1.0
*/
public class TestGsonAPI { @Test
public void test() {
try {
URL url = new URL("http://api.avatardata.cn/TechNews/Query?key=5e3bedcfa2714e36a3e46dd2efce00d9&page=1&rows=10");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// String data = connection.getContentType();
String dataString = new String(GsonTools.IsToByte(connection.getInputStream()),"utf-8");
NewsJson newsJson = GsonTools.getObjectData(dataString, NewsJson.class);
System.out.println(newsJson.toString());
System.out.println(dataString);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

数据太多了,仅仅截图了一部分数据:(图片看不清能够拖动图片到新的页面标签中看哦~)

Gson解析第三方提供Json数据(天气预报,新闻等)的更多相关文章

  1. 使用Gson解析复杂的json数据

    Gson解析复杂的json数据 最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的jso ...

  2. Gson解析复杂的json数据

    最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的json解析方法即JsonObject ...

  3. Gson 解析多层嵌套JSON数据

    http://stackoverflow.com/questions/14139437/java-type-generic-as-argument-for-gson

  4. hive 存储,解析,处理json数据

    hive 处理json数据总体来说有两个方向的路走 1.将json以字符串的方式整个入Hive表,然后通过使用UDF函数解析已经导入到hive中的数据,比如使用LATERAL VIEW json_tu ...

  5. Json1:使用gson解析、生成json

    Json解析: 1.json第三方解析包:json-lib.gson.jackson.fastjson等2.Google-gson只兼容jdk1.5版本以上:JSON-lib分别支持1.4和1.53. ...

  6. 【转】Jquery ajax方法解析返回的json数据

    转自http://blog.csdn.net/haiqiao_2010/article/details/12653555 最近在用jQuery的ajax方法传递接收json数据时发现一个问题,那就是返 ...

  7. $Java-json系列(二):用JSONObject解析和处理json数据

    本文中主要介绍JSONObject处理json数据时候的一些常用场景和方法. (一)jar包下载 所需jar包打包下载百度网盘地址:https://pan.baidu.com/s/1c27Uyre ( ...

  8. 用JSONObject解析和处理json数据

    (一)jar包下载 所需jar包打包下载百度网盘地址:https://pan.baidu.com/s/1c27Uyre (二)常见场景及处理方法 1.解析简单的json字符串: 1 // 简单的jso ...

  9. 【UE4 C++】 解析与构建 Json 数据

    准备条件 Json 格式 { "Players":[ { "Name": "Player1", "health": 20 ...

随机推荐

  1. Math 对象

    Math对象提供了,我们一般进行数学运算的所有函数. Math.random() 随机0~1之间的随机数 [0, 1) Math.max() 求传入参数的最大数 Math.min() 求传入参数的最小 ...

  2. 《剑指offer》-找到字符串中第一个只出现一个的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  3. 百度未授权使用地图API

    百度地图管理员的回复:这是KEY服务升级的问题, 给您造成的不便,非常抱歉.但我们昨日已修复,你可以再审核一番.若不可以,请提供一下您的系统ak,邮箱或qq发送至(wangwenhai@baidu.c ...

  4. java之定时器任务Timer用法

    在项目开发中,经常会遇到需要实现一些定时操作的任务,写过很多遍了,然而每次写的时候,总是会对一些细节有所遗忘,后来想想可能是没有总结的缘故,所以今天小编就打算总结一下可能会被遗忘的小点: 1. pub ...

  5. Codeforces Round #467 (Div. 2) E -Lock Puzzle

    Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...

  6. 【Java】 剑指offer(64) 求1+2+…+n

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 求1+2+…+n,要求不能使用乘除法.for.while.if ...

  7. 072 HBase的架构以及各个模块的功能

    一:整体架构 1.体系结构 2.物理模型 3.存储体系 regionserver—>region->多个store(列簇)->一个memstore和多个storefile 4.HDF ...

  8. macos下mongoDB 3.4.5 添加用户、设置权限

    macos下mongoDB 3.4.5 添加用户.设置权限   在项目中需要根据项目运行环境访问,以不同的身份访问各自的db,所以研究了一下MongoDB的 需求: 给MongoDB添加两个用户分别用 ...

  9. Python3 卷积神经网络卷积层,池化层,全连接层前馈实现

    # -*- coding: utf-8 -*- """ Created on Sun Mar 4 09:21:41 2018 @author: markli " ...

  10. Avahi DOS攻击broadcast-avahi-dos

    Avahi DOS攻击broadcast-avahi-dos   Avahi是Linux下常用的类DNS服务.它可以帮助主机在没有DNS服务的局域网中,发现基于Zeroconf协议的设备和服务.该工具 ...