Json解析tool工具
时间:2014-04-29 13:37:21
收藏:0
阅读:1039
JsonTools
package com.example.weather_json.tools; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.example.weather_json.Entity.Results; import com.example.weather_json.Entity.WeatherData; import com.example.weather_json.Entity.WeatherInfo; public class JsonTools { public static WeatherInfo MyGetJSON(String jsonString) throws JSONException { List<Results> listResults = null; List<WeatherData> listDatas = null; Results results = null; WeatherData weatherData = null; WeatherInfo weatherInfo = null; JSONObject jsonObject = new JSONObject(jsonString); // 解析WeatherInfo对象 weatherInfo = new WeatherInfo(); weatherInfo.setError(jsonObject.getString("error")); weatherInfo.setStatus(jsonObject.getString("status")); weatherInfo.setDate(jsonObject.getString("date")); // 解析Results对象 listResults = new ArrayList<Results>(); JSONArray jsonResArray = jsonObject.getJSONArray("results"); for (int i = 0; i < jsonResArray.length(); i++) { results = new Results(); JSONObject temp = (JSONObject) jsonResArray.opt(i); results.setCurrentCity(temp.getString("currentCity")); // 解析WeatherData对象数组 JSONArray jsonArray = temp.getJSONArray("weather_data"); listDatas = new ArrayList<WeatherData>(); for (int i1 = 0; i1 < jsonArray.length(); i1++) { weatherData = new WeatherData(); JSONObject temp1 = (JSONObject) jsonArray.opt(i1); weatherData.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")); listDatas.add(weatherData); weatherData = null; } results.setList(listDatas); listResults.add(results); results = null; } weatherInfo.setList(listResults); return weatherInfo; } }解析对应的json
{ "error": 0, "status": "success", "date": "2014-04-28", "results": [ { "currentCity": "合肥", "weather_data": [ { "date": "周一(今天, 实时:22℃)", "dayPictureUrl": "www.baidu.com", "nightPictureUrl": "www.baidu.com", "weather": "多云转晴", "wind": "西北风微风", "temperature": "22 ~ 13℃" }, { "date": "周二", "dayPictureUrl": "www.baidu.com", "nightPictureUrl": "1www.baidu.com", "weather": "多云", "wind": "西北风微风", "temperature": "24 ~ 14℃" }, { "date": "周三", "dayPictureUrl": "www.baidu.com", "nightPictureUrl": "www.baidu.com", "weather": "多云转晴", "wind": "南风微风", "temperature": "26 ~ 14℃" }, { "date": "周四", "dayPictureUrl": "www.baidu.com", "nightPictureUrl": "www.baidu.com", "weather": "晴", "wind": "东南风微风", "temperature": "27 ~ 13℃" } ] } ] }
json对象在花括号中
{ "firstName":"John" , "lastName":"Doe" }
json数组在中括号中
{ "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] }
在上面的例子中,对象 "employees" 是包含三个对象的数组。每个对象代表一条关于某人(有姓和名)的记录。
评论(0)