json数据解析成java对象(国家气象局数据)

来源:互联网 发布:单片机和pc通信 编辑:程序博客网 时间:2024/06/06 03:22

网友翻出来的国家气象局天气预报查询出来的数据是这样的。然后看的像JSON 然后感觉哪里不对 最后就把他当做JSON玩。。

找啊找 找到了一个工具 Gson 然后就 上代码吧 

1.weatherinfo 天气预报类

public class weatherinfo {private String city;//:"南京",private String city_en;//":"nanjing",private String date_y;//":"2014年3月4日",private String date;//":"",private String week;//":"星期二",private String fchh;//":"11",private String cityid;//":"101190101",private String temp1;//":"12℃~4℃",private String temp2;//":"12℃~2℃",private String temp3;//":"6℃~1℃",private String temp4;//:"9℃~3℃",private String temp5;//:"8℃~3℃",private String temp6;//:"12℃~3℃",private String tempF1;//:"53.6℉~39.2℉",    private String tempF2;//:"53.6℉~35.6℉",    private String tempF3;//:"42.8℉~33.8℉",    private String tempF4;//:"48.2℉~37.4℉",    private String tempF5;//:"46.4℉~37.4℉",    private String tempF6;//:"53.6℉~37.4℉",    private String weather1;//:"阴转多云",    private String weather2;//:"多云转阴",    private String weather3;//:"小雨转阴",    private String weather4;//:"小雨",    private String weather5;//:"小雨转阴",    private String weather6;//:"多云",    private String img1;//:"2",    private String img2;//:"1",    private String img3;//:"1",    private String img4;//:"2",    private String img5;//:"7",    private String img6;//:"2",    private String img7;//:"7",    private String img8;//:"99",    private String img9;//:"7",    private String img10;//:"2",    private String img11;//:"1",    private String img12;//:"99",    private String img_single;//:"2",    private String img_title1;//:"阴",    private String img_title2;//:"多云",    private String img_title3;//:"多云",    private String img_title4;//:"阴",    private String img_title5;//:"小雨",    private String img_title6;//:"阴",    private String img_title7;//:"小雨",    private String img_title8;//:"小雨",    private String img_title9;//:"小雨",    private String img_title10;//:"阴",    private String img_title11;//:"多云",    private String img_title12;//:"多云",    private String img_title_single;//:"阴",    private String wind1;//:"东风转东北风3-4级",    private String wind2;//:"东北风转东风4-5级",    private String wind3;//:"东风4-5级转3-4级",    private String wind4;//:"东风4-5级",    private String wind5;//:"东风转东北风3-4级",    private String wind6;//:"东南风转南风3-4级",    private String fx1;//:"东风",    private String fx2;//:"东北风",    private String fl1;//:"3-4级",    private String fl2;//:"4-5级",    private String fl3;//:"4-5级转3-4级",    private String fl4;//:"4-5级",    private String fl5;//:"3-4级",    private String fl6;//:"3-4级",    private String index;//:"较冷",    private String index_d;//:"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。",    private String index48;//:"冷",    private String index48_d;//:"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。",    private String index_uv;//:"最弱",    private String index48_uv;//:"最弱",    private String index_xc;//:;//较不宜",    private String index_tr;//:"适宜",    private String index_co;//:"较舒适",    private String st1;//:"13",    private String st2;//:"0",    private String st3;//:"11",    private String st4;//:"0",    private String st5;//:"3",    private String st6;//:"-2",    private String index_cl;//:"较不宜",    private String index_ls;//:"不太适宜",    private String index_ag;//:"易发"    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;}public String getTempF5() {return tempF5;}public void setTempF5(String tempF5) {this.tempF5 = tempF5;}public String getTempF6() {return tempF6;}public void setTempF6(String tempF6) {this.tempF6 = tempF6;}public String getWeather1() {return weather1;}public void setWeather1(String weather1) {this.weather1 = weather1;}public String getWeather2() {return weather2;}public void setWeather2(String weather2) {this.weather2 = weather2;}public String getWeather3() {return weather3;}public void setWeather3(String weather3) {this.weather3 = weather3;}public String getWeather4() {return weather4;}public void setWeather4(String weather4) {this.weather4 = weather4;}public String getWeather5() {return weather5;}public void setWeather5(String weather5) {this.weather5 = weather5;}public String getWeather6() {return weather6;}public void setWeather6(String weather6) {this.weather6 = weather6;}public String getImg1() {return img1;}public void setImg1(String img1) {this.img1 = img1;}public String getImg2() {return img2;}public void setImg2(String img2) {this.img2 = img2;}public String getImg3() {return img3;}public void setImg3(String img3) {this.img3 = img3;}public String getImg4() {return img4;}public void setImg4(String img4) {this.img4 = img4;}public String getImg5() {return img5;}public void setImg5(String img5) {this.img5 = img5;}public String getImg6() {return img6;}public void setImg6(String img6) {this.img6 = img6;}public String getImg7() {return img7;}public void setImg7(String img7) {this.img7 = img7;}public String getImg8() {return img8;}public void setImg8(String img8) {this.img8 = img8;}public String getImg9() {return img9;}public void setImg9(String img9) {this.img9 = img9;}public String getImg10() {return img10;}public void setImg10(String img10) {this.img10 = img10;}public String getImg11() {return img11;}public void setImg11(String img11) {this.img11 = img11;}public String getImg12() {return img12;}public void setImg12(String img12) {this.img12 = img12;}public String getImg_single() {return img_single;}public void setImg_single(String img_single) {this.img_single = img_single;}public String getImg_title1() {return img_title1;}public void setImg_title1(String img_title1) {this.img_title1 = img_title1;}public String getImg_title2() {return img_title2;}public void setImg_title2(String img_title2) {this.img_title2 = img_title2;}public String getImg_title3() {return img_title3;}public void setImg_title3(String img_title3) {this.img_title3 = img_title3;}public String getImg_title4() {return img_title4;}public void setImg_title4(String img_title4) {this.img_title4 = img_title4;}public String getImg_title5() {return img_title5;}public void setImg_title5(String img_title5) {this.img_title5 = img_title5;}public String getImg_title6() {return img_title6;}public void setImg_title6(String img_title6) {this.img_title6 = img_title6;}public String getImg_title7() {return img_title7;}public void setImg_title7(String img_title7) {this.img_title7 = img_title7;}public String getImg_title8() {return img_title8;}public void setImg_title8(String img_title8) {this.img_title8 = img_title8;}public String getImg_title9() {return img_title9;}public void setImg_title9(String img_title9) {this.img_title9 = img_title9;}public String getImg_title10() {return img_title10;}public void setImg_title10(String img_title10) {this.img_title10 = img_title10;}public String getImg_title11() {return img_title11;}public void setImg_title11(String img_title11) {this.img_title11 = img_title11;}public String getImg_title12() {return img_title12;}public void setImg_title12(String img_title12) {this.img_title12 = img_title12;}public String getImg_title_single() {return img_title_single;}public void setImg_title_single(String img_title_single) {this.img_title_single = img_title_single;}public String getWind1() {return wind1;}public void setWind1(String wind1) {this.wind1 = wind1;}public String getWind2() {return wind2;}public void setWind2(String wind2) {this.wind2 = wind2;}public String getWind3() {return wind3;}public void setWind3(String wind3) {this.wind3 = wind3;}public String getWind4() {return wind4;}public void setWind4(String wind4) {this.wind4 = wind4;}public String getWind5() {return wind5;}public void setWind5(String wind5) {this.wind5 = wind5;}public String getWind6() {return wind6;}public void setWind6(String wind6) {this.wind6 = wind6;}public String getFx1() {return fx1;}public void setFx1(String fx1) {this.fx1 = fx1;}public String getFx2() {return fx2;}public void setFx2(String fx2) {this.fx2 = fx2;}public String getFl1() {return fl1;}public void setFl1(String fl1) {this.fl1 = fl1;}public String getFl2() {return fl2;}public void setFl2(String fl2) {this.fl2 = fl2;}public String getFl3() {return fl3;}public void setFl3(String fl3) {this.fl3 = fl3;}public String getFl4() {return fl4;}public void setFl4(String fl4) {this.fl4 = fl4;}public String getFl5() {return fl5;}public void setFl5(String fl5) {this.fl5 = fl5;}public String getFl6() {return fl6;}public void setFl6(String fl6) {this.fl6 = fl6;}public String getIndex() {return index;}public void setIndex(String index) {this.index = index;}public String getIndex_d() {return index_d;}public void setIndex_d(String index_d) {this.index_d = index_d;}public String getIndex48() {return index48;}public void setIndex48(String index48) {this.index48 = index48;}public String getIndex48_d() {return index48_d;}public void setIndex48_d(String index48_d) {this.index48_d = index48_d;}public String getIndex_uv() {return index_uv;}public void setIndex_uv(String index_uv) {this.index_uv = index_uv;}public String getIndex48_uv() {return index48_uv;}public void setIndex48_uv(String index48_uv) {this.index48_uv = index48_uv;}public String getIndex_xc() {return index_xc;}public void setIndex_xc(String index_xc) {this.index_xc = index_xc;}public String getIndex_tr() {return index_tr;}public void setIndex_tr(String index_tr) {this.index_tr = index_tr;}public String getIndex_co() {return index_co;}public void setIndex_co(String index_co) {this.index_co = index_co;}public String getSt1() {return st1;}public void setSt1(String st1) {this.st1 = st1;}public String getSt2() {return st2;}public void setSt2(String st2) {this.st2 = st2;}public String getSt3() {return st3;}public void setSt3(String st3) {this.st3 = st3;}public String getSt4() {return st4;}public void setSt4(String st4) {this.st4 = st4;}public String getSt5() {return st5;}public void setSt5(String st5) {this.st5 = st5;}public String getSt6() {return st6;}public void setSt6(String st6) {this.st6 = st6;}public String getIndex_cl() {return index_cl;}public void setIndex_cl(String index_cl) {this.index_cl = index_cl;}public String getIndex_ls() {return index_ls;}public void setIndex_ls(String index_ls) {this.index_ls = index_ls;}public String getIndex_ag() {return index_ag;}public void setIndex_ag(String index_ag) {this.index_ag = index_ag;}}
2.weatherinfoList 预报集合类

public class weatherinfoList {weatherinfo weatherinfo;public weatherinfo getWeatherinfo() {return weatherinfo;}public void setWeatherinfo(weatherinfo weatherinfo) {this.weatherinfo = weatherinfo;}}

3.getDate数据获取

import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import org.json.JSONException;public class getDate {public static final String GET_URL = " http://m.weather.com.cn/data/101190101.html";     public static final String POST_URL = " http://localhost:8080/demo/  ";     public static void main(String[] args) {         // TODO Auto-generated method stub         try {                 try {readContentFromGet();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}                 //readContentFromPost();         } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();         } }    public static String readContentFromGet() throws IOException, JSONException {            // 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码            String getURL = GET_URL;            URL getUrl = new URL(getURL);            // 根据拼凑的URL,打开连接,URL.openConnection()函数会根据 URL的类型,返回不同的URLConnection子类的对象,在这里我们的URL是一个http,因此它实际上返回的是HttpURLConnection            HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();            //connection.setRequestProperty("Charset", "utf-8");           // 建立与服务器的连接,并未发送数据            connection.connect();            // 发送数据到服务器并使用Reader读取返回的数据            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));            System.out.println(" ============================= ");            System.out.println(" Contents of get request ");            System.out.println(" ============================= ");            /*            * {            *             * "weatherinfo":            {"city":"南京",            "city_en":"nanjing",            "date_y":"2014年3月4日",            "date":"",            "week":"星期二",            "fchh":"11",            "cityid":"101190101",            "temp1":"12℃~4℃",            "temp2":"12℃~2℃",            "temp3":"6℃~1℃",            "temp4":"9℃~3℃",            "temp5":"8℃~3℃",            "temp6":"12℃~3℃",            "tempF1":"53.6℉~39.2℉",            "tempF2":"53.6℉~35.6℉",            "tempF3":"42.8℉~33.8℉",            "tempF4":"48.2℉~37.4℉",            "tempF5":"46.4℉~37.4℉",            "tempF6":"53.6℉~37.4℉",            "weather1":"阴转多云",            "weather2":"多云转阴",            "weather3":"小雨转阴",            "weather4":"小雨",            "weather5":"小雨转阴",            "weather6":"多云",            "img1":"2",            "img2":"1",            "img3":"1",            "img4":"2",            "img5":"7",            "img6":"2",            "img7":"7",            "img8":"99",            "img9":"7",            "img10":"2",            "img11":"1",            "img12":"99",            "img_single":"2",            "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-4级",            "wind2":"东北风转东风4-5级",            "wind3":"东风4-5级转3-4级",            "wind4":"东风4-5级",            "wind5":"东风转东北风3-4级",            "wind6":"东南风转南风3-4级",            "fx1":"东风",            "fx2":"东北风",            "fl1":"3-4级",            "fl2":"4-5级",            "fl3":"4-5级转3-4级",            "fl4":"4-5级",            "fl5":"3-4级",            "fl6":"3-4级",            "index":"较冷",            "index_d":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。",            "index48":"冷",            "index48_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。",            "index_uv":"最弱",            "index48_uv":"最弱",            "index_xc":"较不宜",            "index_tr":"适宜",            "index_co":"较舒适",            "st1":"13",            "st2":"0",            "st3":"11",            "st4":"0",            "st5":"3",            "st6":"-2",            "index_cl":"较不宜",            "index_ls":"不太适宜",            "index_ag":"易发"            }            * }            * */           String str=reader.readLine();           reader.close();           // 断开连接           connection.disconnect();                       System.out.println(" ============================= ");            System.out.println(" Contents of get request ends ");            System.out.println(" ============================= ");return str;     }     public static void readContentFromPost() throws IOException {            // Post请求的url,与get不同的是不需要带参数            URL postUrl = new URL(POST_URL);            // 打开连接            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();            //打开读写属性,默认均为false            connection.setDoOutput(true);                      connection.setDoInput(true);            // 设置请求方式,默认为GET            connection.setRequestMethod(" POST ");            // Post 请求不能使用缓存            connection.setUseCaches(false);            // URLConnection.setFollowRedirects是static 函数,作用于所有的URLConnection对象。            // connection.setFollowRedirects(true);            //URLConnection.setInstanceFollowRedirects 是成员函数,仅作用于当前函数            connection.setInstanceFollowRedirects(true);            // 配置连接的Content-type,配置为application/x- www-form-urlencoded的意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码            connection.setRequestProperty(" Content-Type ",                            " application/x-www-form-urlencoded ");            // 连接,从postUrl.openConnection()至此的配置必须要在 connect之前完成,            // 要注意的是connection.getOutputStream()会隐含的进行调用 connect(),所以这里可以省略            //connection.connect();            DataOutputStream out = new DataOutputStream(connection                            .getOutputStream());            //正文内容其实跟get的URL中'?'后的参数字符串一致            String content = " firstname= "+URLEncoder.encode(" 一个大肥人 ", " utf-8 ");            // DataOutputStream.writeBytes将字符串中的16位的 unicode字符以8位的字符形式写道流里面            out.writeBytes(content);            out.flush();            out.close(); // flush and close            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));            String line;            System.out.println(" ============================= ");            System.out.println(" Contents of post request ");            System.out.println(" ============================= ");            while ((line = reader.readLine()) != null) {                    System.out.println(line);            }            System.out.println(" ============================= ");            System.out.println(" Contents of post request ends ");            System.out.println(" ============================= ");            reader.close();            //connection.disconnect();     }  }

4.方法测试


import java.io.IOException;import org.json.JSONException;import com.google.gson.Gson;public class weatherinfoToJavaBean {public static void main(String[] args) throws IOException, JSONException {        String str=getDate.readContentFromGet();        Gson gson = new Gson();        weatherinfoList winfo=gson.fromJson(str, weatherinfoList.class);        System.out.println(winfo.getWeatherinfo().getCity());}}

5.输出截图



在这里感谢由忘记是谁提供的 http请求方法 

0 0
原创粉丝点击