激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java中Json字符串直接轉換為對象的方法(包括多層List集合)

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

2020-06-07 12:24jingxian JAVA教程

下面小編就為大家帶來一篇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

下面是例子代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
// 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類代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
    }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
    }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
    }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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字符串

?
1
{"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集合)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人午夜天堂 | 欧美成人性生活片 | 福利在线免费 | 午夜视频中文字幕 | 男人天堂免费 | 毛片一级片| 欧美成人一区二区三区 | 国内精品国产三级国产a久久 | 国产xxxx岁13xxxxhd | a视频在线播放 | 一区二区三区四区免费 | 免费观看视频在线 | 空姐毛片 | 手机在线看片国产 | 香蕉黄色网 | 国产一区日韩一区 | 中文字幕伦乱 | 99精品视频免费看 | 黄色av免费网站 | 国产精品成人久久久久a级 av电影在线免费 | 超久久 | 国产三级在线视频观看 | 国产精品99久久久久久大便 | 韩国精品视频在线观看 | 福利在线国产 | 成人国产在线看 | 黄色片网站在线免费观看 | 亚洲一级电影在线观看 | hdhdhdhd19日本人 | 伊人成人免费视频 | 国产精品亚洲yourport | 久久国产综合精品 | 毛片大全在线观看 | 国产午夜精品久久久久久久蜜臀 | 一日本道久久久精品国产 | 我爱我色成人网 | 成人 日韩| 国产一国产精品一级毛片 | 国产在线观看一区二区三区 | 久久久青 | 羞羞视频免费观看网站 |