欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年08月24日 10:42:10   投稿:jingxian  
下面小編就為大家?guī)硪黄狫ava中Json字符串直接轉換為對象的方法(包括多層List集合)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

使用JSON時,除了要導入JSON網(wǎng)站上面下載的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;//狀態(tài)值
	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;//指數(shù)集合
	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;//指數(shù)簡述
	private String des;//指數(shù)概述

	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":"穿衣指數(shù)","des":"建議著厚外套加毛衣等服裝。年老體弱者宜著大衣、呢外套加羊毛衫。"},{"title":"洗車","zs":"較適宜","tipt":"洗車指數(shù)","des":"較適宜洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。"},{"title":"旅游","zs":"適宜","tipt":"旅游指數(shù)","des":"天氣較好,氣溫稍低,會感覺稍微有點涼,不過也是個好天氣哦。適宜旅游,可不要錯過機會呦!"},{"title":"感冒","zs":"少發(fā)","tipt":"感冒指數(shù)","des":"各項氣象條件適宜,無明顯降溫過程,發(fā)生感冒機率較低。"},{"title":"運動","zs":"較不宜","tipt":"運動指數(shù)","des":"陰天,且天氣寒冷,推薦您在室內(nèi)進行低強度運動;若堅持戶外運動,請選擇合適的運動并注意保暖。"},{"title":"紫外線強度","zs":"最弱","tipt":"紫外線強度指數(shù)","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":"穿衣指數(shù)","des":"建議著厚外套加毛衣等服裝。年老體弱者宜著大衣、呢外套加羊毛衫。"},{"title":"洗車","zs":"較適宜","tipt":"洗車指數(shù)","des":"較適宜洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。"},{"title":"旅游","zs":"適宜","tipt":"旅游指數(shù)","des":"天氣較好,但絲毫不會影響您出行的心情。溫度適宜又有微風相伴,適宜旅游。"},{"title":"感冒","zs":"較易發(fā)","tipt":"感冒指數(shù)","des":"天氣較涼,較易發(fā)生感冒,請適當增加衣服。體質(zhì)較弱的朋友尤其應該注意防護。"},{"title":"運動","zs":"較不宜","tipt":"運動指數(shù)","des":"天氣較好,但考慮天氣寒冷,推薦您進行各種室內(nèi)運動,若在戶外運動請注意保暖并做好準備活動。"},{"title":"紫外線強度","zs":"最弱","tipt":"紫外線強度指數(shù)","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集合)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java中&和&&的區(qū)別簡單介紹

    Java中&和&&的區(qū)別簡單介紹

    這篇文章主要介紹了Java中&和&&的區(qū)別,&&邏輯與||邏輯或  它們都是邏輯運算符,& 按位與|按位或它們都是位運算符,更多詳細內(nèi)容請需要的小伙伴了解下面文章內(nèi)容
    2022-01-01
  • 手把手帶你理解java線程池之工作隊列workQueue

    手把手帶你理解java線程池之工作隊列workQueue

    這篇文章主要介紹了java線程池之工作隊列workQueue,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • mybatis?報錯顯示sql中有兩個limit的解決

    mybatis?報錯顯示sql中有兩個limit的解決

    這篇文章主要介紹了mybatis?報錯顯示sql中有兩個limit的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 工廠方法模式_動力節(jié)點Java學院整理

    工廠方法模式_動力節(jié)點Java學院整理

    這篇文章主要介紹了工廠方法模式_動力節(jié)點Java學院整理的相關資料,需要的朋友可以參考下
    2017-08-08
  • Java反射機制用法總結

    Java反射機制用法總結

    反射機制是在運行狀態(tài)中,對于任意一個類,都能夠知道這個類的所有屬性和方法;對于任意一個對象,都能夠調(diào)用它的任意一個方法和屬性;這種動態(tài)獲取的信息以及動態(tài)調(diào)用對象的方法的功能稱為java語言的反射機制。下面我們來一起學習一下吧
    2019-05-05
  • 淺談java封裝

    淺談java封裝

    封裝封裝就是將屬性私有化,提供公有的方法訪問私有的屬性。*實現(xiàn)封裝的步驟:(1)修改屬性的可見性來限制對屬性的訪問。(2)為每個屬性創(chuàng)建一對賦值方法和取值方法,用于對這些屬性的訪問。(3)在賦值和取值方法中,加入對屬性的存取限制。
    2015-03-03
  • mybatis插入與批量插入返回ID的原理詳解

    mybatis插入與批量插入返回ID的原理詳解

    這篇文章主要給大家介紹了關于mybatis插入與批量插入返回ID的原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用mybatis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • Springboot使用redis實現(xiàn)接口Api限流的示例代碼

    Springboot使用redis實現(xiàn)接口Api限流的示例代碼

    本文主要介紹了Springboot使用redis實現(xiàn)接口Api限流的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • java整合微信支付功能詳細示例

    java整合微信支付功能詳細示例

    這篇文章主要給大家介紹了關于java整合微信支付功能的相關資料,支付是一個復雜且測試起來需要的配置特別復雜的模塊,文中給出了詳細的示例代碼,需要的朋友可以參考下
    2023-07-07
  • Java?Chassis3負載均衡選擇器技術解密

    Java?Chassis3負載均衡選擇器技術解密

    這篇文章主要為大家介紹了Java?Chassis3負載均衡選擇器技術解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01

最新評論