Android parses the JSON data of the China Weather Network

Source: Internet
Author: User

In Android development, the general app is to get server-side data to update the UI, from the server to obtain the data can be JSON, its data is smaller than XML, the analysis of the Chinese weather online access to the data, although it is no longer updated, but to learn is still possible, In order to facilitate my direct to the data through the TXT saved to the local, its data can be obtained through this page: http://m.weather.com.cn/data/101280601.html

Directory structure


In fact, the analysis is also very simple, the key code

private void Json3 () {InputStream inputstream = null; String Weatherinfo = null;try {System.out.println ("-------------------------------->json3"); InputStream = Getresources (). Getassets (). Open ("Weather.txt"); weatherinfo = convertstreamtostring (InputStream);} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} Gets the Jsonobject object jsonobject jsonobject = null; Jsonobject jsoncity = null;try {jsonobject = new Jsonobject (weatherinfo); jsoncity = new Jsonobject (jsonobject.getString ( "Weatherinfo")); Weatherinfo info = new Weatherinfo (); info.city = jsoncity.getstring ("city"); Info.cityid = jsoncity.getstring ("Cityid") ; info.city_en = jsoncity.getstring ("city_en"); info.date = jsoncity.getstring ("date"); info.date_y = Jsoncity.getstring ("date_y"); Info.fchh = jsoncity.getstring ("Fchh"); info.fl[0] = jsoncity.getstring ("Fl1"); info.fl [1] = jsoncity.getstring ("FL2"); info.fl[2] = jsoncity.getstring ("Fl3"); info.fl[3] = jsoncity.getstring ("Fl4"); INFO.FL[4] = jsoncity.getstring ("Fl5"); Info.fl[5] = jsoncity.getstring ("fl6"); info.fx[0] = jsoncity.getstring ("fx1"); info.fx[1] = jsoncity.getstring ("fx2 "); info.img[0] = jsoncity.getstring (" Img1 "); info.img[1] = jsoncity.getstring (" Img2 "); info.img[2] = Jsoncity.getstring ("Img3"); info.img[3] = jsoncity.getstring ("Img4"); Info.img[4] = jsoncity.getstring ("Img5"); INFO.IMG[5] = jsoncity.getstring ("Img6"); Info.img[6] = jsoncity.getstring ("Img7"); Info.img[7] = Jsoncity.getstring (" Img8 "); Info.img[8] = jsoncity.getstring (" Img9 "); info.img[9] = jsoncity.getstring (" Img10 "); info.img[10] = Jsoncity.getstring ("img11"); info.img[11] = jsoncity.getstring ("Img12"); Info.img_single = jsoncity.getstring ("Img_ Single "); Info.img_title[0] = jsoncity.getstring (" Img_title1 "); info.img_title[1] = jsoncity.getstring (" Img_title2 ") ; info.img_title[2] = jsoncity.getstring ("Img_title3"); info.img_title[3] = jsoncity.getstring ("Img_title4"); INFO.IMG_TITLE[4] = jsoncity.getstring ("Img_title5"); info.img_title[5] = jsoncity.getstring ("Img_title6"); info.img _title[6] = jsoncity.getstring ("Img_title7"); Info.img_title[7] = jsoncity.getstring ("Img_title8"); info.img_title[8] = Jsoncity.getstring ("Img_title9"); info.img_title[9] = jsoncity.getstring ("Img_title10"); info.img_title[10] = Jsoncity.getstring ("Img_title11"); info.img_title[11] = jsoncity.getstring ("Img_title12"); Info.img_title_single = Jsoncity.getstring ("Img_title_single"); info.temp[0] = jsoncity.getstring ("Temp1"); info.temp[1] = Jsoncity.getstring ("Temp2"); info.temp[2] = jsoncity.getstring ("Temp3"); info.temp[3] = jsoncity.getstring ("Temp4"); INFO.TEMP[4] = jsoncity.getstring ("Temp5"); info.temp[5] = jsoncity.getstring ("temp6"); info.tempf[0] = Jsoncity.getstring ("tempF1"); info.tempf[1] = jsoncity.getstring ("tempF2"); info.tempf[2] = Jsoncity.getstring (" TempF3 "); info.tempf[3] = jsoncity.getstring (" tempF4 "); Info.tempf[4] = jsoncity.getstring (" tempF5 "); info.tempf[5] = Jsoncity.getstring ("tempF6"); info.weather[0] = jsoncity.getstring ("Weather1"); info.weather[1] = jsoncity.getstring ("Weather2"); infO.WEATHER[2] = jsoncity.getstring ("Weather3"); info.weather[3] = jsoncity.getstring ("Weather4"); info.weather[4] = Jsoncity.getstring ("Weather5"); info.weather[5] = jsoncity.getstring ("Weather6"); Info.week = Jsoncity.getstring (" Week "); info.wind[0] = jsoncity.getstring (" Wind1 "); info.wind[1] = jsoncity.getstring (" Wind2 "); info.wind[2] = Jsoncity.getstring ("Wind3"); info.wind[3] = jsoncity.getstring ("Wind4"); Info.wind[4] = jsoncity.getstring ("Wind5"); INFO.WIND[5] = jsoncity.getstring ("Wind6"); info.img_title[0] = jsoncity.getstring ("Img_title1"); SYSTEM.OUT.PRINTLN ("JSON =" + info.tostring ());} catch (Jsonexception e) {e.printstacktrace ();}}
I've encapsulated the weather information separately.

Package Com.example.jsondemo.domain;import Java.util.arrays;public class Weatherinfo {public String city;public string Cityid;public string City_en;public string Date;public string Date_y;public string Fchh;public string fl[];p ublic string f X[];p ublic string img[];p ublic string img_single;public string img_title[];p ublic string Img_title_single;public string Index;public string Index48;public string Index48_d;public string Index48_uv;public string Index_ag;public string index_ Cl;public string Index_co;public string Index_d;public string Index_ls;public string Index_tr;public string Index_uv; public string Index_xc;public string st[];p ublic string temp[];p ublic string tempf[];p ublic string weather[];p ublic Strin G Week;public String wind[];p ublic weatherinfo () {fl = new String[6];fx = new String[2];img = new String[12];img_title = n EW string[12];st = new String[6];temp = new String[6];tempf = new String[6];weather = new String[6];wind = new String[6];} @Overridepublic String toString () {RETurn "Weatherinfo [city=" + City + ", cityid=" + cityid+ ", city_en=" + City_en + ", date=" + Date + ", date_y=" + date_y + ", fchh=" + Fchh + ", fl=" + arrays.tostring (fl) + ", fx=" + arrays.tostring (FX) + ", img=" + arrays.tostring (IMG) + ", img  _single= "+ img_single+", img_title= "+ arrays.tostring (img_title) +", img_title_single= "+ Img_title_single +", index= "  + index+ ", index48=" + index48 + ", index48_d=" + index48_d+ ", index48_uv=" + Index48_uv + ", index_ag=" + index_ag+ ", Index_cl= "+ Index_cl +", index_co= "+ index_co+", index_d= "+ Index_d +", index_ls= "+ index_ls+", index_tr= "+ Inde X_tr + ", index_uv=" + index_uv+ ", index_xc=" + Index_xc + ", st=" + arrays.tostring (ST) + ", temp=" + arrays.tostring (tem p) + ", tempf=" + arrays.tostring (Tempf) + ", weather=" + arrays.tostring (Weather) + ", week=" + Week + ", wind=" + arrays.to String (Wind) + "]";}}

The parsed result


There are several web sites for JSON data that can be verified, and the validation is not a canonical JSON format

http://jsonviewer.stack.hu/

http://json.parser.online.fr/

These sites can format JSON data to look more convenient

If the format is wrong, there will be hints, convenient for us to modify, it is important to note that the creation of the file to save JSON data can not have a newline

The following is wrong, it is for our convenience to understand

{"Weatherinfo": {"City": "Shenzhen", "city_en": "Shenzhen", "date_y": "March 4, 2014", "date": "", "Week": "Tuesday", "Fchh": "One", "Cityid": "101280601", "Temp1": "20℃~15℃", "Temp2": "20℃~15℃", "Temp3": "21℃~16℃", "Temp4" : "19℃~16℃", "TEMP5": "20℃~15℃", "TEMP6": "19℃~14℃", "tempF1": "68℉~59℉", "tempF2": "68℉~59℉", "tempF3": "6 9.8℉~60.8℉ "," tempF4 ":" 66.2℉~60.8℉ "," tempF5 ":" 68℉~59℉ "," tempF6 ":" 66.2℉~57.2℉ "," Weather1 ":" Cloudy "," weat HER2 ":" Light Rain "," Weather3 ":" Light Rain "," weather4 ":" Yin Turn Light Rain "," weather5 ":" Drizzle "," weather6 ":" Drizzle Turn yin "," IMG1 ":" 1 "," I Mg2 ":" "IMG3": "7", "IMG4": "The", "Img5": "7", "Img6": "2", "Img7": "7", "Img8": " 7 "," Img10 ":" 7 "," Img11 ":" 2 "," Img_single ":" 1 "," Img_title1 ":" Cloudy "," Img_title2 ":" Cloudy " , "Img_title3": "Drizzle", "img_title4": "Drizzle", "img_title5": "Drizzle", "img_title6": "Drizzle", "img_title7": "Yin", "im G_title8 ":" Drizzle "," img_title9 ":" Light Rain "," img_title10 ":" Drizzle "," img_title11 ":" Drizzle "," img_title12 ":" Yin "," img_title_single ":" Cloudy "," WI    Nd1 ":" Breeze "," wind2 ":" Breeze "," Wind3 ":" Dongfeng 3-4 Level "," Wind4 ":" Breeze "," Wind5 ":" Breeze "," Wind6 ":" Breeze "," fx1 ":" Breeze ", "FX2": "Breeze", "fl1": "Less than 3 levels", "FL2": "Less than 3 levels", "FL3": "3-4 Levels", "FL4": "Less than 3 levels", "FL5": "Less than 3 levels", "fl6": "Less than 3 levels" , "index": "More Comfortable", "index_d": "Recommended thin jacket, cardigan denim shirt pants and other clothing." The frail and infirm should be suitably added clothing, suitable for jackets, thin sweaters and so on. "," index48 ":" More Comfortable "," index48_d ":" Suggest a thin jacket, cardigan denim shirt pants and other clothing. " The frail and infirm should be suitably added clothing, suitable for jackets, thin sweaters and so on. "," INDEX_UV ":" Weak "," INDEX48_UV ":" Weakest "," index_xc ":" Unsuitable "," index_tr ":" Suitable "," index_co ":" Comfortable "," st1 ":" 20 "," St2 ":" The "," St3 ":" "," "St4": "", "" "", "St5": "", "St6": "", "INDEX_CL": "Suitable", "Index_ls": "Suitable "," Index_ag ":" Very easy to send "}}
The following format is correct

{"Weatherinfo": {"City": "Shenzhen", "city_en": "Shenzhen", "date_y": "March 4, 2014", "date": "", "Week": "Tuesday", "Fchh": "11", " Cityid ":" 101280601 "," Temp1 ":" 20℃~15℃ "," Temp2 ":" 20℃~15℃ "," Temp3 ":" 21℃~16℃ "," Temp4 ":" 19℃~16℃ "," TEMP5 ":" 20℃~15℃ ", "TEMP6": "19℃~14℃", "tempF1": "68℉~59℉", "tempF2": "68℉~59℉", "tempF3": "69.8℉~60.8℉", "tempF4": "66.2℉~60.8℉", "tempF5" : "68℉~59℉", "tempF6": "66.2℉~57.2℉", "Weather1": "Cloudy", "Weather2": "Light Rain", "Weather3": "Light Rain", "weather4": "Yin Turn drizzle", "Weather5" : "Light Rain", "weather6": "Drizzle Turn yin", "IMG1": "1", "Img2": "The", "IMG3": "7", "Img4": "7", "Img5": "The", "Img6": "2", "" Img7 " : "7", "Img9": "7", "Img10": ",", "img11": "7", "Img12": "2", "Img_single": "1", "Img_title1": "Cloudy", "Img_title2": "Cloudy", "img _title3 ":" Light Rain "," img_title4 ":" Light Rain "," img_title5 ":" Light Rain "," img_title6 ":" Drizzle "," img_title7 ":" Yin "," img_title8 ":" Drizzle "," img_ Title9 ":" Light Rain "," Img_title10 ":" Light Rain "," img_title11 ":" Drizzle "," img_title12 ":" Yin "," img_title_single ":" Cloudy "," wind1 ":" Breeze "," Wind2 ":" Breeze "," Wind3 ":" Dongfeng 3-4 Level "," Wind4 ":" Breeze "," Wind5 ":" Breeze "," Wind6 ":" Breeze "," fx1 ":" Breeze "," fx2 ":" Breeze "," fl1 ":" Less than 3 levels "," FL2 ":" Less than Level 3 "," FL3 ":" Level 3-4 ","Fl4": "Less than 3 levels", "FL5": "Less than 3 levels", "fl6": "Less than 3 levels", "index": "More Comfortable", "index_d": "Recommended thin jacket, cardigan denim shirt pants and other clothing." The frail and infirm should be suitably added clothing, suitable for jackets, thin sweaters and so on. "," index48 ":" More Comfortable "," index48_d ":" Suggest a thin jacket, cardigan denim shirt pants and other clothing. " The frail and infirm should be suitably added clothing, suitable for jackets, thin sweaters and so on. "," INDEX_UV ":" Weak "," INDEX48_UV ":" Weakest "," INDEX_XC ":" Inappropriate "," INDEX_TR ":" Suitable "," index_co ":" Comfortable "," st1 ":" The "," St2 ":" The "," ST3 " : "+", "St4": "St5", "St6": "+", "INDEX_CL": "Suitable", "Index_ls": "Suitable", "Index_ag": "Very easy to send"}}

Demo Download: http://download.csdn.net/detail/deng0zhaotai/7741419

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.