1. 程式人生 > >Java中Json字串直接轉換為物件(包括多層List集合)

Java中Json字串直接轉換為物件(包括多層List集合)

使用到的類:net.sf.json.JSONObject 

使用JSON時,除了要匯入JSON網站上面下載的json-lib-2.2-jdk15.jar包之外,還必須有其它幾個依賴包:commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar

下面是例子程式碼:

				// JSON轉換
				JSONObject jsonObj = JSONObject.fromObject(jsonStrBody);
				
				Map<String, Class> classMap = new HashMap<String, Class>();
	            classMap.put("results", WeatherBean_Baidu_City.class);
	            classMap.put("index", WeatherBean_Baidu_City_Index.class);  
	            classMap.put("weather_data", WeatherBean_Baidu_City_Weatherdata.class);  
	            // 將JSON轉換成WeatherBean_Baidu  
	            WeatherBean_Baidu weather = (WeatherBean_Baidu) JSONObject.toBean(jsonObj,  
	            		WeatherBean_Baidu.class, classMap); 
	            System.out.println(weather.getResults());

使用到的幾個JAVA類程式碼:
package com.lenovo.conference.entity.vo;

import java.io.Serializable;
import java.util.List;

/**
 * 天氣Bean
 * 
 * @author SHANHY
 * 
 */
@SuppressWarnings("serial")
public class WeatherBean_Baidu implements Serializable {

	private String error;//錯誤號
	private String status;//狀態值
	private String date;//日期
	private List<WeatherBean_Baidu_City> results;//城市天氣預報集合(因為一次可以查詢多個城市)

	public WeatherBean_Baidu() {
		super();
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public List<WeatherBean_Baidu_City> getResults() {
		return results;
	}

	public void setResults(List<WeatherBean_Baidu_City> results) {
		this.results = results;
	}

}

package com.lenovo.conference.entity.vo;

import java.io.Serializable;
import java.util.List;

/**
 * 天氣Bean
 * 
 * @author SHANHY
 * 
 */
@SuppressWarnings("serial")
public class WeatherBean_Baidu_City implements Serializable {

	private String currentCity;//城市名稱
	private String pm25;//pm2.5值
	private List<WeatherBean_Baidu_City_Index> index;//指數集合
	private List<WeatherBean_Baidu_City_Weatherdata> weather_data;//幾天的天氣集合

	public WeatherBean_Baidu_City() {
		super();
	}

	public String getCurrentCity() {
		return currentCity;
	}

	public void setCurrentCity(String currentCity) {
		this.currentCity = currentCity;
	}

	public String getPm25() {
		return pm25;
	}

	public void setPm25(String pm25) {
		this.pm25 = pm25;
	}

	public List<WeatherBean_Baidu_City_Index> getIndex() {
		return index;
	}

	public void setIndex(List<WeatherBean_Baidu_City_Index> index) {
		this.index = index;
	}

	public List<WeatherBean_Baidu_City_Weatherdata> getWeather_data() {
		return weather_data;
	}

	public void setWeather_data(
			List<WeatherBean_Baidu_City_Weatherdata> weather_data) {
		this.weather_data = weather_data;
	}

}

package com.lenovo.conference.entity.vo;

import java.io.Serializable;

/**
 * 天氣Bean
 * 
 * @author SHANHY
 * 
 */
@SuppressWarnings("serial")
public class WeatherBean_Baidu_City_Weatherdata implements Serializable {

	private String date;// 日期
	private String dayPictureUrl;// 白天的天氣圖片
	private String nightPictureUrl;// 晚上的天氣圖片
	private String weather;// 天氣
	private String wind;// 風向
	private String temperature;// 溫度

	public WeatherBean_Baidu_City_Weatherdata() {
		super();
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDayPictureUrl() {
		return dayPictureUrl;
	}

	public void setDayPictureUrl(String dayPictureUrl) {
		this.dayPictureUrl = dayPictureUrl;
	}

	public String getNightPictureUrl() {
		return nightPictureUrl;
	}

	public void setNightPictureUrl(String nightPictureUrl) {
		this.nightPictureUrl = nightPictureUrl;
	}

	public String getWeather() {
		return weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public String getWind() {
		return wind;
	}

	public void setWind(String wind) {
		this.wind = wind;
	}

	public String getTemperature() {
		return temperature;
	}

	public void setTemperature(String temperature) {
		this.temperature = temperature;
	}

}

package com.lenovo.conference.entity.vo;

import java.io.Serializable;

/**
 * 天氣Bean
 * 
 * @author SHANHY
 * 
 */
@SuppressWarnings("serial")
public class WeatherBean_Baidu_City_Index implements Serializable {

	private String title;//標題
	private String zs;//舒適度
	private String tipt;//指數簡述
	private String des;//指數概述

	public WeatherBean_Baidu_City_Index() {
		super();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getZs() {
		return zs;
	}

	public void setZs(String zs) {
		this.zs = zs;
	}

	public String getTipt() {
		return tipt;
	}

	public void setTipt(String tipt) {
		this.tipt = tipt;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}

}

例子中解析所對應的JSON字串
{"error":0,"status":"success","date":"2015-01-15","results":[{"currentCity":"南京","pm25":"83","index":[{"title":"穿衣","zs":"較冷","tipt":"穿衣指數","des":"建議著厚外套加毛衣等服裝。年老體弱者宜著大衣、呢外套加羊毛衫。"},{"title":"洗車","zs":"較適宜","tipt":"洗車指數","des":"較適宜洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。"},{"title":"旅遊","zs":"適宜","tipt":"旅遊指數","des":"天氣較好,氣溫稍低,會感覺稍微有點涼,不過也是個好天氣哦。適宜旅遊,可不要錯過機會呦!"},{"title":"感冒","zs":"少發","tipt":"感冒指數","des":"各項氣象條件適宜,無明顯降溫過程,發生感冒機率較低。"},{"title":"運動","zs":"較不宜","tipt":"運動指數","des":"陰天,且天氣寒冷,推薦您在室內進行低強度運動;若堅持戶外運動,請選擇合適的運動並注意保暖。"},{"title":"紫外線強度","zs":"最弱","tipt":"紫外線強度指數","des":"屬弱紫外線輻射天氣,無需特別防護。若長期在戶外,建議塗擦SPF在8-12之間的防晒護膚品。"}],"weather_data":[{"date":"週四 01月15日 (實時:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"陰轉多雲","wind":"北風微風","temperature":"8 ~ 4℃"},{"date":"週五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"多雲轉晴","wind":"西北風3-4級","temperature":"12 ~ 0℃"},{"date":"週六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"晴轉多雲","wind":"東北風3-4級","temperature":"8 ~ 0℃"},{"date":"週日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"西風微風","temperature":"10 ~ -1℃"}]},{"currentCity":"徐州","pm25":"154","index":[{"title":"穿衣","zs":"較冷","tipt":"穿衣指數","des":"建議著厚外套加毛衣等服裝。年老體弱者宜著大衣、呢外套加羊毛衫。"},{"title":"洗車","zs":"較適宜","tipt":"洗車指數","des":"較適宜洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。"},{"title":"旅遊","zs":"適宜","tipt":"旅遊指數","des":"天氣較好,但絲毫不會影響您出行的心情。溫度適宜又有微風相伴,適宜旅遊。"},{"title":"感冒","zs":"較易發","tipt":"感冒指數","des":"天氣較涼,較易發生感冒,請適當增加衣服。體質較弱的朋友尤其應該注意防護。"},{"title":"運動","zs":"較不宜","tipt":"運動指數","des":"天氣較好,但考慮天氣寒冷,推薦您進行各種室內運動,若在戶外運動請注意保暖並做好準備活動。"},{"title":"紫外線強度","zs":"最弱","tipt":"紫外線強度指數","des":"屬弱紫外線輻射天氣,無需特別防護。若長期在戶外,建議塗擦SPF在8-12之間的防晒護膚品。"}],"weather_data":[{"date":"週四 01月15日 (實時:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多雲","wind":"南風微風","temperature":"10 ~ 3℃"},{"date":"週五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多雲","wind":"北風3-4級","temperature":"11 ~ -4℃"},{"date":"週六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多雲","wind":"東風微風","temperature":"6 ~ -4℃"},{"date":"週日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多雲","wind":"西風3-4級","temperature":"11 ~ -1℃"}]}]}