解析的数据:
{
"citycode": "101051301",
"citydesc": "黑龙江 双鸭山",
"cityname": "双鸭山",
"data": [
{
"date": "2015-08-29",
"icon": "d07|n07",
"temperature": "20°C/16°C",
"weather": "小雨",
"winddirect": "东风"
},
{
"date": "2015-08-30",
"icon": "d07|n07",
"temperature": "21°C/16°C",
"weather": "小雨",
"winddirect": "东风3-4级转微风"
},
{
"date": "2015-08-31",
"icon": "d07|n01",
"temperature": "23°C/16°C",
"weather": "小雨转多云",
"winddirect": "东风转东南风微风"
},
{
"date": "2015-09-01",
"icon": "d01|n01",
"temperature": "24°C/16°C",
"weather": "多云",
"winddirect": "东南风转南风微风"
},
{
"date": "2015-09-02",
"icon": "d03|n03",
"temperature": "25°C/16°C",
"weather": "阵雨",
"winddirect": "南风转东北风微风"
},
{
"date": "2015-09-03",
"icon": "d03|n01",
"temperature": "24°C/15°C",
"weather": "阵雨转多云",
"winddirect": "东北风转北风微风"
}
],
"lastupdate": "2015-08-29 18:13:50",
"live": {
"humidity": "48%",
"temperature": "3℃",
"updatetime": "10:00",
"winddirect": "西南风2级"
},
"publishtime": "2015年08月29日18:00"
}
创建工具类
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class HttpUtil { //得到服务器端的json字符串 public static String getJsonString(String path) throws IOException { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setDoInput(true); if(conn.getResponseCode()==200) { InputStream in = conn.getInputStream(); return changeToString(in); } return null; } private static String changeToString(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] arr = new byte[1024]; int len = 0; while((len = in.read(arr))!=-1) { bos.write(arr,0,len); } return new String(bos.toByteArray()); } //解析json字符串,返回Person对象的集合 public static Weather jsonParser(String jsonString) throws JSONException { Weather weather = new Weather(); JSONObject obj = new JSONObject(jsonString); String cityname = obj.getString("cityname"); String citycode = obj.getString("citycode"); String citydesc = obj.getString("citydesc"); String publishtime = obj.getString("publishtime"); String lastupdate = obj.getString("lastupdate"); weather.setCityName(cityname); weather.setCityCode(citycode); weather.setCityDesc(citydesc); weather.setPublishTime(publishtime); weather.setLastUpdate(lastupdate); JSONArray array = obj.getJSONArray("data"); List<WeatherData> data = new ArrayList<WeatherData>(); for(int i=0;i<array.length();i++) { JSONObject jsonObj = array.getJSONObject(i); String date = jsonObj.getString("date"); String icon = jsonObj.getString("icon"); String wea = jsonObj.getString("weather"); String temperature = jsonObj.getString("temperature"); String winddirect = jsonObj.getString("winddirect"); WeatherData wd = new WeatherData(); wd.setDate(date); wd.setIcon(icon); wd.setWeather(wea); wd.setTemperature(temperature); wd.setWinddirect(winddirect); data.add(wd); } weather.setData(data); JSONObject jsonObject = obj.getJSONObject("live"); String updatetime = jsonObject.getString("updatetime"); String temperature = jsonObject.getString("temperature"); String humidity = jsonObject.getString("humidity"); String winddirect = jsonObject.getString("winddirect"); Live live = new Live(); live.setUpdateTime(updatetime); live.setTemperature(temperature); live.setHumidity(humidity); live.setWinddirect(winddirect); weather.setLive(live); return weather; } }
import java.util.List; public class Weather { private String cityName; private String cityCode; private String cityDesc; private String publishTime; private String lastUpdate; private List<WeatherData> data; private Live live; public Weather() { super(); // TODO Auto-generated constructor stub } public Weather(String cityName, String cityCode, String cityDesc, String publishTime, String lastUpdate, List<WeatherData> data, Live live) { super(); this.cityName = cityName; this.cityCode = cityCode; this.cityDesc = cityDesc; this.publishTime = publishTime; this.lastUpdate = lastUpdate; this.data = data; this.live = live; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getCityCode() { return cityCode; } public void setCityCode(String cityCode) { this.cityCode = cityCode; } public String getCityDesc() { return cityDesc; } public void setCityDesc(String cityDesc) { this.cityDesc = cityDesc; } public String getPublishTime() { return publishTime; } public void setPublishTime(String publishTime) { this.publishTime = publishTime; } public String getLastUpdate() { return lastUpdate; } public void setLastUpdate(String lastUpdate) { this.lastUpdate = lastUpdate; } public List<WeatherData> getData() { return data; } public void setData(List<WeatherData> data) { this.data = data; } public Live getLive() { return live; } public void setLive(Live live) { this.live = live; } @Override public String toString() { return "Weather [cityName=" + cityName + ", cityCode=" + cityCode + ", cityDesc=" + cityDesc + ", publishTime=" + publishTime + ", lastUpdate=" + lastUpdate + ", data=" + data + ", live=" + live + "]"; } }
public class Live { private String updateTime; private String temperature; private String humidity; private String winddirect; public Live() { super(); // TODO Auto-generated constructor stub } public Live(String updateTime, String temperature, String humidity, String winddirect) { super(); this.updateTime = updateTime; this.temperature = temperature; this.humidity = humidity; this.winddirect = winddirect; } public String getUpdateTime() { return updateTime; } public void setUpdateTime(String updateTime) { this.updateTime = updateTime; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getHumidity() { return humidity; } public void setHumidity(String humidity) { this.humidity = humidity; } public String getWinddirect() { return winddirect; } public void setWinddirect(String winddirect) { this.winddirect = winddirect; } @Override public String toString() { return "Live [updateTime=" + updateTime + ", temperature=" + temperature + ", humidity=" + humidity + ", winddirect=" + winddirect + "]"; } }
public class WeatherData { private String date; private String icon; private String weather; private String temperature; private String winddirect; public WeatherData() { super(); // TODO Auto-generated constructor stub } public WeatherData(String date, String icon, String weather, String temperature, String winddirect) { super(); this.date = date; this.icon = icon; this.weather = weather; this.temperature = temperature; this.winddirect = winddirect; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWinddirect() { return winddirect; } public void setWinddirect(String winddirect) { this.winddirect = winddirect; } @Override public String toString() { return "WeatherData [date=" + date + ", icon=" + icon + ", weather=" + weather + ", temperature=" + temperature + ", winddirect=" + winddirect + "]"; } }
测试:
import java.io.IOException; import org.json.JSONException; public class Test { /** * @param args * @throws IOException * @throws JSONException */ public static void main(String[] args) throws IOException, JSONException { String path = "http://weather.xcyh.org/101051301/json/6"; String jsonStr = HttpUtil.getJsonString(path); System.out.println(jsonStr); Weather weather = HttpUtil.jsonParser(jsonStr); System.out.println(weather); } }