Android parses Json data of China weather network
In Android development, a general APP updates the UI by obtaining server-side data. The data obtained from the server can be Json, and the data size is smaller than that of XML, here, we will analyze the data obtained on the Chinese weather network. Although it is no longer updated, it is still possible to learn it. For convenience, I can directly store the data to the local through txt, its data can be obtained through this page: http://m.weather.com.cn/data/101280601.html
Directory structure
In fact, the resolution is also very simple, key code
Private void json3 () {InputStream inputStream = null; String weatherinfo = null; try {System. out. println (pipeline> json3); inputStream = pipeline); weatherinfo = convertStreamToString (inputStream);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} // obtain 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 encapsulate 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[];public String fx[];public String img[];public String img_single;public String img_title[];public 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[];public String temp[];public String tempF[];public String weather[];public String week;public String wind[];public WeatherInfo() {fl = new String[6];fx = new String[2];img = new String[12];img_title = new 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= + index_tr + , index_uv= + index_uv+ , index_xc= + index_xc + , st= + Arrays.toString(st)+ , temp= + Arrays.toString(temp) + , tempF=+ Arrays.toString(tempF) + , weather=+ Arrays.toString(weather) + , week= + week + , wind=+ Arrays.toString(wind) + ];}}
Parsed result
Several websites can check Json data to see if the Json format is standard.
Http://jsonviewer.stack.hu/
Http://json.parser.online.fr/
These websites can format Json data for convenience.
If the format is incorrect, a prompt will be displayed to facilitate modification. Note that line breaks are not allowed when creating a file to save Json data.
Which of the following is false for our convenience?
{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 bytes, tempF2: 68 bytes ~ 59 Gbit/s, tempF3: 69.8 Gbit/s ~ 60.8 bytes, tempF4: 66.2 bytes ~ 60.8 bytes, tempF5: 68 bytes ~ 59 bytes, tempF6: 66.2 bytes ~ 57.2 Gbit/s, weather1: Cloudy, weather2: light rain, weather3: light rain, weather4: overcast to light rain, weather5: light rain, weather6: light rain to overcast, img1: 1, img2: 99, img3: 7, img4: 99, img5: 7, img6: 99, img7: 2, img8: 7, img9: 7, img10: 99, img11: 7, img12: 2, img_single: 1. Moderate: Cloudy, moderate: Cloudy, img_title3: light rain, img_title4: light rain, img_title5: light rain, moderate: light rain, overcast, img_title8: light rain, img_title9: light rain, img_title10: light rain, img_title11: light rain, Img_title12: overcast, img_title_single: Cloudy, wind1: breeze, wind2: breeze, wind3: Dongfeng 3-4, wind4: breeze, wind5: breeze, wind6: breeze, fx1: breeze, fx2: breeze, fl1: less than 3, fl2: less than 3, fl3: 3-4, fl4: less than 3, fl5: less than 3, fl6: less than 3, index: more comfortable, index_d: it is recommended to wear thin coat, cardigan denim shirt pants and other clothing. Old and weak people should add appropriate clothing, and should wear jackets and thin sweaters ., Index48: relatively comfortable, index48_d: it is recommended to wear thin coat, cardigan denim shirt pants and other clothing. Old and weak people should add appropriate clothing, and should wear jackets and thin sweaters ., Index_uv: weak, index48_uv: weakest, index_xc: not suitable, index_tr: suitable, index_co: comfortable, st1: 20, st2: 15, st3: 21, st4: 15, st5: 21, st6: 14, index_cl: suitable, index_ls: suitable, index_ag: extremely easy }}
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 bytes, tempF2: 68 bytes ~ 59 Gbit/s, tempF3: 69.8 Gbit/s ~ 60.8 bytes, tempF4: 66.2 bytes ~ 60.8 bytes, tempF5: 68 bytes ~ 59 bytes, tempF6: 66.2 bytes ~ 57.2 Gbit/s, weather1: Cloudy, weather2: light rain, weather3: light rain, weather4: overcast to light rain, weather5: light rain, weather6: light rain to overcast, img1: 1, img2: 99, img3: 7, img4: 99, img5: 7, img6: 99, img7: 2, img8: 7, img9: 7, img10: 99, img11: 7, img12: 2, img_single: 1. Moderate: Cloudy, moderate: Cloudy, img_title3: light rain, img_title4: light rain, img_title5: light rain, moderate: light rain, overcast, img_title8: light rain, img_title9: light rain, img_title10: light rain, img_title11: light rain, img_title12: overcast, img_title_single: Cloudy, wind1: breeze, wind2: breeze, wind3: Dongfeng 3-4, wind4: breeze, wind5: breeze, wind6: breeze, fx1: breeze, fx2: breeze, fl1: less than 3, fl2: less than 3, fl3: 3-4, fl4: less than 3 levels, fl5: less than 3 levels, fl6: less than 3 levels, index: more comfortable, index_d: recommended for clothing such as thin coat and cardigan denim shirt. Old and weak people should add appropriate clothing, and should wear jackets and thin sweaters ., Index48: relatively comfortable, index48_d: it is recommended to wear thin coat, cardigan denim shirt pants and other clothing. Old and weak people should add appropriate clothing, and should wear jackets and thin sweaters ., Index_uv: weak, index48_uv: weakest, index_xc: not suitable, index_tr: suitable, index_co: comfortable, st1: 20, st2: 15, st3: 21, st4: 15, st5: 21, st6: 14, index_cl: suitable, index_ls: suitable, index_ag: extremely easy }}