Android development tool Class 14_ Jsontools

Source: Internet
Author: User

Weather JSON Data parsing

1  PackageCom.example.weather_json.tools;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 ImportOrg.json.JSONArray;7 Importorg.json.JSONException;8 ImportOrg.json.JSONObject;9 Ten ImportCom.example.weather_json. Entity.results; One ImportCom.example.weather_json. Entity.weatherdata; A ImportCom.example.weather_json. Entity.weatherinfo; -  -  Public classJsontools { the      Public StaticWeatherinfo Mygetjson (String jsonstring)throwsjsonexception { -List<results> Listresults =NULL; -List<weatherdata> Listdatas =NULL; -Results Results =NULL; +Weatherdata Weatherdata =NULL; -Weatherinfo Weatherinfo =NULL; +Jsonobject Jsonobject =NewJsonobject (jsonstring); A  at         //parsing Weatherinfo Objects -Weatherinfo =Newweatherinfo (); -Weatherinfo.seterror (jsonobject.getstring ("Error")); -Weatherinfo.setstatus (jsonobject.getstring ("status")); -Weatherinfo.setdate (jsonobject.getstring ("date")); -         //parsing Results Objects inListresults =NewArraylist<results>(); -Jsonarray Jsonresarray = Jsonobject.getjsonarray ("Results"); to          for(inti = 0; I < jsonresarray.length (); i++) { +Results =NewResults (); -Jsonobject temp =(jsonobject) jsonresarray.opt (i); theResults.setcurrentcity (temp.getstring ("CurrentCity")); *  $             //parsing an array of Weatherdata objectsPanax NotoginsengJsonarray Jsonarray = Temp.getjsonarray ("Weather_data"); -Listdatas =NewArraylist<weatherdata>(); the              for(intI1 = 0; I1 < Jsonarray.length (); i1++) { +Weatherdata =NewWeatherdata (); AJsonobject Temp1 =(Jsonobject) jsonarray.opt (i1); theWeatherdata.setdate (temp1.getstring ("date")); +Weatherdata.setdaypictureurl (temp1.getstring ("Daypictureurl")); - Weatherdata.setnightpictureurl (Temp1 $. getString ("Nightpictureurl")); $Weatherdata.setweather (temp1.getstring ("Weather")); -Weatherdata.setwind (Temp1.getstring ("Wind")); -Weatherdata.settemperature (temp1.getstring ("Temperature")); the Listdatas.add (weatherdata); -Weatherdata =NULL;Wuyi             } the results.setlist (listdatas); - Listresults.add (results); WuResults =NULL; -         } About weatherinfo.setlist (listresults); $         returnWeatherinfo; -     } -}

JSON object

1 {2"Error": 0,3"Status": "Success",4"Date": "2014-04-28",5"Results": [6         {7"CurrentCity": "Hefei",8"Weather_data": [9                 {Ten"Date": "Monday (Today, Live: 22 ℃)", One"Daypictureurl": "Www.baidu.com", A"Nightpictureurl": "Www.baidu.com", -"Weather": "Cloudy to clear", -"Wind": "Northwest Breeze", the"Temperature": "22 ~ 13 ℃" -                 }, -                 { -"Date": "Tuesday", +"Daypictureurl": "Www.baidu.com", -"Nightpictureurl": "1www.baidu.com", +"Weather": "Cloudy", A"Wind": "Northwest Breeze", at"Temperature": "24 ~ 14 ℃" -                 }, -                 { -"Date": "Wednesday", -"Daypictureurl": "Www.baidu.com", -"Nightpictureurl": "Www.baidu.com", in"Weather": "Cloudy to clear", -"Wind": "Southerly Breeze", to"Temperature": "26 ~ 14 ℃" +                 }, -                 { the"Date": "Thursday", *"Daypictureurl": "Www.baidu.com", $"Nightpictureurl": "Www.baidu.com",Panax Notoginseng"Weather": "Sunny", -"Wind": "Southeast Wind Breeze", the"Temperature": "27 ~ 13 ℃" +                 } A             ] the         } +     ] -}

"References"

http://blog.csdn.net/u010794950/article/details/24650421

Android development tool Class 14_ Jsontools

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.