国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中Json字符串直接轉換為對象的方法(包括多層List集合)

2019-11-26 13:56:04
字體:
來源:轉載
供稿:網友

使用到的類: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℃"}]}]}

以上這篇Java中Json字符串直接轉換為對象的方法(包括多層List集合)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昭通市| 克什克腾旗| 云林县| 维西| 武汉市| 图片| 明光市| 忻城县| 新昌县| 江川县| 巴塘县| 嘉鱼县| 西安市| 綦江县| 高密市| 弥渡县| 湛江市| 明水县| 长白| 滕州市| 尖扎县| 延吉市| 洛阳市| 和林格尔县| 宜良县| 西宁市| 屯门区| 略阳县| 翼城县| 肇源县| 阿瓦提县| 呼图壁县| 丁青县| 东莞市| 通州市| 庄浪县| 杭锦旗| 灵寿县| 扶风县| 万州区| 镇雄县|