Java中Json字符串直接轉換為對象的辦法(包含多層List聚集)。本站提示廣大學習愛好者:(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":"建議著厚外衣加毛衣等服裝網www.vhao.net。年邁體弱者宜著年夜衣、呢外衣加羊毛衫。"},{"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.百度.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"陰轉多雲","wind":"寒風輕風","temperature":"8 ~ 4℃"},{"date":"周五","dayPictureUrl":"http://api.map.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/qing.png","weather":"多雲放晴","wind":"東南風3-4級","temperature":"12 ~ 0℃"},{"date":"周六","dayPictureUrl":"http://api.map.百度.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"晴轉多雲","wind":"西南風3-4級","temperature":"8 ~ 0℃"},{"date":"周日","dayPictureUrl":"http://api.map.百度.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/qing.png","weather":"晴","wind":"西風輕風","temperature":"10 ~ -1℃"}]},{"currentCity":"徐州","pm25":"154","index":[{"title":"穿衣","zs":"較冷","tipt":"穿衣指數","des":"建議著厚外衣加毛衣等服裝網www.vhao.net。年邁體弱者宜著年夜衣、呢外衣加羊毛衫。"},{"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.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"多雲","wind":"熏風輕風","temperature":"10 ~ 3℃"},{"date":"周五","dayPictureUrl":"http://api.map.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"多雲","wind":"寒風3-4級","temperature":"11 ~ -4℃"},{"date":"周六","dayPictureUrl":"http://api.map.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"多雲","wind":"春風輕風","temperature":"6 ~ -4℃"},{"date":"周日","dayPictureUrl":"http://api.map.百度.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.百度.com/images/weather/night/duoyun.png","weather":"多雲","wind":"西風3-4級","temperature":"11 ~ -1℃"}]}]}
以上這篇Java中Json字符串直接轉換為對象的辦法(包含多層List聚集)就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。