国家气象局天气预报为我们提供了数据交换格式为JSON的WEB API:http://m.weather.com.cn/data/101210101.html
数字代码代表不同的城市和其它城镇,具体对应关系请查阅博客:http://blog.csdn.net/zgyulongfei/article/details/7956118
我们可以利用URL和HttpURLConnection(在移动平台,比如android,可以使用HttpGet来获取数据流)获取Web Service为我们提供的JSON数据。
但是我们知道Java中都是对象,而JSON是一个有具体格式的字符串,我们需要进行相应的转换。
我们可以利用org.json.*包或者google提供的gson包来解析JSON数据,这里我们使用google的gson包吧。
gson包的下载地址为:
假设我们已经获得了JSON数据,比如:
{"weatherinfo":{"city":"杭州","city_en":"hangzhou","date_y":"2013年2月28日","date":"","week":"星期四","fchh":"11","cityid":"101210101","temp1":"21℃~10℃","temp2":"11℃~4℃","temp3":"6℃~2℃","temp4":"10℃~3℃","temp5":"15℃~3℃","temp6":"16℃~4℃","tempF1":"69.8℉~50℉","tempF2":"51.8℉~39.2℉","tempF3":"42.8℉~35.6℉","tempF4":"50℉~37.4℉","tempF5":"59℉~37.4℉","tempF6":"60.8℉~39.2℉","weather1":"多云转中雨","weather2":"中雨转小雨","weather3":"阴转多云","weather4":"晴转多云","weather5":"晴","weather6":"多云","img1":"1","img2":"8","img3":"8","img4":"7","img5":"2","img6":"1","img7":"0","img8":"1","img9":"0","img10":"99","img11":"1","img12":"99","img_single":"1","img_title1":"多云","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":"阴","img_title6":"多云","img_title7":"晴","img_title8":"多云","img_title9":"晴","img_title10":"晴","img_title11":"多云","img_title12":"多云","img_title_single":"多云","wind1":"东北风小于3级","wind2":"北风小于3级","wind3":"北风小于3级","wind4":"东北风小于3级","wind5":"东北风转东风小于3级","wind6":"东风小于3级","fx1":"东北风","fx2":"东北风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"较冷","index_d":"建议着大衣、呢外套加毛衣、卫衣等服装。体弱者宜着厚外套、厚毛衣。因昼夜温差较大,注意增减衣服。","index48":"较冷","index48_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index_uv":"最弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"舒适","st1":"22","st2":"10","st3":"9","st4":"2","st5":"5","st6":"2","index_cl":"适宜","index_ls":"适宜","index_ag":"极不易发"}}
注意这是一个JSON字符串,{}代表一个对象,[]代表一个数组,"key":"value"代表对象的一个属性,只要知道这些基本常识就可以了。
现在我们需要在Java中编写该JSON数据对应的POJO类:
观察该JSON数据字符串可以知道,该对象只有一个属性weatherinfo,而weatherinfo又是一个对象,weatherinfo对象包含许多类型为字符串的属性。
先编写最外部的类吧,我们把它命名成WeatherStation:
public class WeatherStation {
private WeatherInfo weatherinfo;
public WeatherInfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(WeatherInfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
接下来是编写WeatherInfo类:(这里只写了一部分属性)
public class WeatherInfo {
private String city;
private String city_en;
private String date_y;
private String date;
private String week;
private String fchh;
private String cityid;
private String temp1;
private String temp2;
private String temp3;
private String temp4;
private String temp5;
private String temp6;
private String tempF1;
private String tempF2;
private String tempF3;
private String tempF4;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCity_en() {
return city_en;
}
public void setCity_en(String city_en) {
this.city_en = city_en;
}
public String getDate_y() {
return date_y;
}
public void setDate_y(String date_y) {
this.date_y = date_y;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getFchh() {
return fchh;
}
public void setFchh(String fchh) {
this.fchh = fchh;
}
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public String getTemp1() {
return temp1;
}
public void setTemp1(String temp1) {
this.temp1 = temp1;
}
public String getTemp2() {
return temp2;
}
public void setTemp2(String temp2) {
this.temp2 = temp2;
}
public String getTemp3() {
return temp3;
}
public void setTemp3(String temp3) {
this.temp3 = temp3;
}
public String getTemp4() {
return temp4;
}
public void setTemp4(String temp4) {
this.temp4 = temp4;
}
public String getTemp5() {
return temp5;
}
public void setTemp5(String temp5) {
this.temp5 = temp5;
}
public String getTemp6() {
return temp6;
}
public void setTemp6(String temp6) {
this.temp6 = temp6;
}
public String getTempF1() {
return tempF1;
}
public void setTempF1(String tempF1) {
this.tempF1 = tempF1;
}
public String getTempF2() {
return tempF2;
}
public void setTempF2(String tempF2) {
this.tempF2 = tempF2;
}
public String getTempF3() {
return tempF3;
}
public void setTempF3(String tempF3) {
this.tempF3 = tempF3;
}
public String getTempF4() {
return tempF4;
}
public void setTempF4(String tempF4) {
this.tempF4 = tempF4;
}
}
好了,万事俱备,只欠东风,下面我们利用gson包提供的Gson类的parseJson和fromJson方法来实现解析和反解析:
public class Main2 {
/**
* @param args
*/
public static void main(String[] args) {
String json = getJSONString();
Gson gson = new Gson();
WeatherStation ws = gson.fromJson(json, WeatherStation.class);
System.out.println(ws.getWeatherinfo().getCity());
System.out.println(ws.getWeatherinfo().getCity_en());
System.out.println(ws.getWeatherinfo().getTempF3());
System.out.println(ws.getWeatherinfo().getTempF4());
}
private static String getJSONString() {
try {
URL url = new URL("http://m.weather.com.cn/data/101210101.html");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String msg = null;
StringBuilder sb = new StringBuilder();
while((msg = br.readLine()) != null) {
sb.append(msg);
}
return sb.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
看看输出:
杭州
hangzhou
42.8℉~35.6℉
50℉~37.4℉
import java.io.IOException;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) throws IOException {
Disney sh = new Disney();
sh.setName("shanghai disney");
sh.setLocation(new Location("shanghai Rd.", 123456));
Disney ny = new Disney();
ny.setName("NewYork disney");
ny.setLocation(new Location("newyork Rd.", 54321));
Disney[] disneys = {sh,ny};
Gson gson = new Gson();
String jsonString = gson.toJson(disneys);
System.out.println(jsonString);
Disney[] disneyss = gson.fromJson(jsonString, Disney[].class);
for(Disney disney : disneyss) {
System.out.println(disney.getName());
System.out.println(disney.getSingleTicket());
for(String interest : disney.getIntrests()) {
System.out.println(interest);
}
System.out.println(disney.getLocation().getStreet());
}
}
}
class Location {
private String street;
private long postcode;
public Location(String street,long postcode) {
this.street = street;
this.postcode = postcode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public long getPostcode() {
return postcode;
}
public void setPostcode(long postcode) {
this.postcode = postcode;
}
}
class Disney {
private String name;
private Location location;
private double singleTicket = 8.8;
private String[] intrests = {"boat","water","sky"};
public String[] getIntrests() {
return intrests;
}
public void setIntrests(String[] intrests) {
this.intrests = intrests;
}
public Disney() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public double getSingleTicket() {
return singleTicket;
}
public void setSingleTicket(double singleTicket) {
this.singleTicket = singleTicket;
}
@Override
public String toString() {
return "Disney [name=" + name + ", location=" + location
+ ", singleTicket=" + singleTicket + "]";
}
}
分享到:
相关推荐
zipkin为分布式链路调用监控系统,聚合各业务系统调用延迟数据,达到链路调用监控跟踪。...3.Query负责查询Storage中存储的数据,提供简单的JSON API获取数据,主要提供给web UI使用 4.Web 提供简单的web界面
Visual C#, IronRuby, Ironpython对JSON、Web Service、WCF以及Sockets支持等系列新特性步步学Silverlight 2系列文章将从Silverlight 2基础知识、数据和通信、自定义Control控件、动画、图形图像等几个方面带您快速...
该解决方案可与存储在Amazon S3存储桶中的Parquet和JSON格式数据一起使用。 您的数据湖通过AWS Glue表并通过指定需要使用表中的哪些列来标识要擦除的数据而连接到解决方案。 配置完成后,您可以将要删除其相应数据...
后端(Python Flask)接收短信数据,进行垃圾短信筛选,返回Json数据给前端: 后端使用scikit-learn模块(数据挖掘和数据分析工具),通过其朴素贝叶斯算法API对短信数据进行垃圾短信的识别: 导入中文垃圾短信数据...
该Java Web服务通过一个简单的JSON响应提供《 Web of Science Times被引次数》。 检查“ example”目录以获取使用javascript的简单实现。 Web of Science的文章匹配检索(AMR)Web服务用于对Web of Science进行实时...
APDPlat提供了应用容器、多模块架构、代码生成、安装程序、认证授权、备份恢复、数据字典、web service、系统监控、操作审计、统计图、报表、机器绑定、防止破解、数据安全、内置搜索、数据转换、maven支持、WEB...
26_采用JSON格式返回数据给资讯客户端.avi 所在项目:news & Web端应用:web 27_网络通信之通过GET和POST方式提交参数给web应用.avi 所在项目:newsmanage & Web端应用:web 28_网络通信之通过HTTP协议实现文件上传....
26_采用JSON格式返回数据给资讯客户端.avi 所在项目:news & Web端应用:web 27_网络通信之通过GET和POST方式提交参数给web应用.avi 所在项目:newsmanage & Web端应用:web 28_网络通信之通过HTTP协议实现文件上传....
26_采用JSON格式返回数据给资讯客户端.avi 所在项目:news & Web端应用:web 27_网络通信之通过GET和POST方式提交参数给web应用.avi 所在项目:newsmanage & Web端应用:web 28_网络通信之通过HTTP协议实现文件上传....
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
Js、css、数据服务(增删改查)、元数据服务(读取、排序)。 11. Nature.Service.SSOAuth 单点登录,服务端的验证功能,也是服务中心的一部分 12. Nature.Service.UserCenter 用户中心,配合单点登录,对用户的...
服务现在RESTful连接器使用 Webservices 连接到 ServiceNow 并返回 JSON 数据首先,使用脚本或更新集生成带有正确注释的 java 类。 接下来,根据需要创建服务类。 最后,创建资源类作为服务的端点。 添加您需要的...
xml java系统源码 zipkin zipkin为分布式链路调用监控系统,聚合各业务系统调用延迟数据,达到链路调用监控跟踪。...API获取数据,主要提供给web UI使用 Web 提供简单的web界面 install 执行如下命令下载jar包 w
8.3.1 JSON数据解析 8.3.2 XML数据解析 8.4 Web Service访问 8.5 小结 练习 第9章 多媒体应用 9.1 音频与视频的播放 9.1.1 MediaPlayer 9.1.2 SoundPool 9.1.3 VideoView 9.1.4 SurfaceView 9.2...
2)后台使用了javaweb,主要是jsp+mvc+servlet分层思想实现,每一个功能都是通过javabean获取前台页面的数据,传递给servlet,然后进一步通过service层与dao数据库层的交互进行功能数据的处理,处理结果通过bean返回...
web service可以执行从简单的请求到复杂商务处理的任何功能。一旦部署以后,其他webse rvice应用程序可以发现并调用它部署的服务。webservice的主要目标就是跨平台的可互 操作性。为了达到这一目标,webservice完全...
12.2 JSON格式 12.2.1 基本类型 12.2.2 数组和集合 12.2.3 类对象 12.3 JSON解析 12.4 Gson 12.4.1 简单对象类型转换 12.4.2 数组和集合类型转换 12.5 xStream 12.6 本章小结 第四篇 Android应用开发案例 第13章 ...