从Web Service获取JSON格式数据
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
国家气象局天气预报为我们提供了数据交换格式为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 + "]";
}
}
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)