Android 开发工具类 14_ JsonTools

天气 JSON 数据解析

 1 package com.example.weather_json.tools;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.json.JSONArray;
 7 import org.json.JSONException;
 8 import org.json.JSONObject;
 9 
10 import com.example.weather_json.Entity.Results;
11 import com.example.weather_json.Entity.WeatherData;
12 import com.example.weather_json.Entity.WeatherInfo;
13 
14 public class JsonTools {
15     public static WeatherInfo MyGetJSON(String jsonString) throws JSONException {
16         List<Results> listResults = null;
17         List<WeatherData> listDatas = null;
18         Results results = null;
19         WeatherData weatherData = null;
20         WeatherInfo weatherInfo = null;
21         JSONObject jsonObject = new JSONObject(jsonString);
22 
23         // 解析WeatherInfo对象
24         weatherInfo = new WeatherInfo();
25         weatherInfo.setError(jsonObject.getString("error"));
26         weatherInfo.setStatus(jsonObject.getString("status"));
27         weatherInfo.setDate(jsonObject.getString("date"));
28         // 解析Results对象
29         listResults = new ArrayList<Results>();
30         JSONArray jsonResArray = jsonObject.getJSONArray("results");
31         for (int i = 0; i < jsonResArray.length(); i++) {
32             results = new Results();
33             JSONObject temp = (JSONObject) jsonResArray.opt(i);
34             results.setCurrentCity(temp.getString("currentCity"));
35 
36             // 解析WeatherData对象数组
37             JSONArray jsonArray = temp.getJSONArray("weather_data");
38             listDatas = new ArrayList<WeatherData>();
39             for (int i1 = 0; i1 < jsonArray.length(); i1++) {
40                 weatherData = new WeatherData();
41                 JSONObject temp1 = (JSONObject) jsonArray.opt(i1);
42                 weatherData.setDate(temp1.getString("date"));
43                 weatherData.setDayPictureUrl(temp1.getString("dayPictureUrl"));
44                 weatherData.setNightPictureUrl(temp1
45                         .getString("nightPictureUrl"));
46                 weatherData.setWeather(temp1.getString("weather"));
47                 weatherData.setWind(temp1.getString("wind"));
48                 weatherData.setTemperature(temp1.getString("temperature"));
49                 listDatas.add(weatherData);
50                 weatherData = null;
51             }
52             results.setList(listDatas);
53             listResults.add(results);
54             results = null;
55         }
56         weatherInfo.setList(listResults);
57         return weatherInfo;
58     }
59 }

json 对象

 1 {
 2     "error": 0,
 3     "status": "success",
 4     "date": "2014-04-28",
 5     "results": [
 6         {
 7             "currentCity": "合肥",
 8             "weather_data": [
 9                 {
10                     "date": "周一(今天, 实时:22℃)",
11                     "dayPictureUrl": "www.baidu.com",
12                     "nightPictureUrl": "www.baidu.com",
13                     "weather": "多云转晴",
14                     "wind": "西北风微风",
15                     "temperature": "22 ~ 13℃"
16                 },
17                 {
18                     "date": "周二",
19                     "dayPictureUrl": "www.baidu.com",
20                     "nightPictureUrl": "1www.baidu.com",
21                     "weather": "多云",
22                     "wind": "西北风微风",
23                     "temperature": "24 ~ 14℃"
24                 },
25                 {
26                     "date": "周三",
27                     "dayPictureUrl": "www.baidu.com",
28                     "nightPictureUrl": "www.baidu.com",
29                     "weather": "多云转晴",
30                     "wind": "南风微风",
31                     "temperature": "26 ~ 14℃"
32                 },
33                 {
34                     "date": "周四",
35                     "dayPictureUrl": "www.baidu.com",
36                     "nightPictureUrl": "www.baidu.com",
37                     "weather": "晴",
38                     "wind": "东南风微风",
39                     "temperature": "27 ~ 13℃"
40                 }
41             ]
42         }
43     ]
44 }

【参考文献】

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

posted @ 2015-05-28 14:31  壬子木  阅读(334)  评论(0编辑  收藏  举报