Java中Json字符串直接转换为对象(包括多层List集合 嵌套)

2024-05-27 07:38

本文主要是介绍Java中Json字符串直接转换为对象(包括多层List集合 嵌套),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/catoop/article/details/42744705

使用到的类: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 

下面是例子代码:

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. // JSON转换  
  2. JSONObject jsonObj = JSONObject.fromObject(jsonStrBody);  
  3.   
  4. Map<String, Class> classMap = new HashMap<String, Class>();  
  5.          classMap.put("results", WeatherBean_Baidu_City.class);  
  6.          classMap.put("index", WeatherBean_Baidu_City_Index.class);    
  7.          classMap.put("weather_data", WeatherBean_Baidu_City_Weatherdata.class);    
  8.          // 将JSON转换成WeatherBean_Baidu    
  9.          WeatherBean_Baidu weather = (WeatherBean_Baidu) JSONObject.toBean(jsonObj,    
  10.                 WeatherBean_Baidu.class, classMap);   
  11.          System.out.println(weather.getResults());  

使用到的几个JAVA类代码:

[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.lenovo.conference.entity.vo;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. /**  
  7.  * 天气Bean  
  8.  *   
  9.  * @author SHANHY  
  10.  *   
  11.  */  
  12. @SuppressWarnings("serial")  
  13. public class WeatherBean_Baidu implements Serializable {  
  14.   
  15.     private String error;//错误号  
  16.     private String status;//状态值  
  17.     private String date;//日期  
  18.     private List<WeatherBean_Baidu_City> results;//城市天气预报集合(因为一次可以查询多个城市)  
  19.   
  20.     public WeatherBean_Baidu() {  
  21.         super();  
  22.     }  
  23.   
  24.     public String getError() {  
  25.         return error;  
  26.     }  
  27.   
  28.     public void setError(String error) {  
  29.         this.error = error;  
  30.     }  
  31.   
  32.     public String getStatus() {  
  33.         return status;  
  34.     }  
  35.   
  36.     public void setStatus(String status) {  
  37.         this.status = status;  
  38.     }  
  39.   
  40.     public String getDate() {  
  41.         return date;  
  42.     }  
  43.   
  44.     public void setDate(String date) {  
  45.         this.date = date;  
  46.     }  
  47.   
  48.     public List<WeatherBean_Baidu_City> getResults() {  
  49.         return results;  
  50.     }  
  51.   
  52.     public void setResults(List<WeatherBean_Baidu_City> results) {  
  53.         this.results = results;  
  54.     }  
  55.   
  56. }  

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.lenovo.conference.entity.vo;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * 天气Bean 
  8.  *  
  9.  * @author SHANHY 
  10.  *  
  11.  */  
  12. @SuppressWarnings("serial")  
  13. public class WeatherBean_Baidu_City implements Serializable {  
  14.   
  15.     private String currentCity;//城市名称  
  16.     private String pm25;//pm2.5值  
  17.     private List<WeatherBean_Baidu_City_Index> index;//指数集合  
  18.     private List<WeatherBean_Baidu_City_Weatherdata> weather_data;//几天的天气集合  
  19.   
  20.     public WeatherBean_Baidu_City() {  
  21.         super();  
  22.     }  
  23.   
  24.     public String getCurrentCity() {  
  25.         return currentCity;  
  26.     }  
  27.   
  28.     public void setCurrentCity(String currentCity) {  
  29.         this.currentCity = currentCity;  
  30.     }  
  31.   
  32.     public String getPm25() {  
  33.         return pm25;  
  34.     }  
  35.   
  36.     public void setPm25(String pm25) {  
  37.         this.pm25 = pm25;  
  38.     }  
  39.   
  40.     public List<WeatherBean_Baidu_City_Index> getIndex() {  
  41.         return index;  
  42.     }  
  43.   
  44.     public void setIndex(List<WeatherBean_Baidu_City_Index> index) {  
  45.         this.index = index;  
  46.     }  
  47.   
  48.     public List<WeatherBean_Baidu_City_Weatherdata> getWeather_data() {  
  49.         return weather_data;  
  50.     }  
  51.   
  52.     public void setWeather_data(  
  53.             List<WeatherBean_Baidu_City_Weatherdata> weather_data) {  
  54.         this.weather_data = weather_data;  
  55.     }  
  56.   
  57. }  

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.lenovo.conference.entity.vo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * 天气Bean 
  7.  *  
  8.  * @author SHANHY 
  9.  *  
  10.  */  
  11. @SuppressWarnings("serial")  
  12. public class WeatherBean_Baidu_City_Weatherdata implements Serializable {  
  13.   
  14.     private String date;// 日期  
  15.     private String dayPictureUrl;// 白天的天气图片  
  16.     private String nightPictureUrl;// 晚上的天气图片  
  17.     private String weather;// 天气  
  18.     private String wind;// 风向  
  19.     private String temperature;// 温度  
  20.   
  21.     public WeatherBean_Baidu_City_Weatherdata() {  
  22.         super();  
  23.     }  
  24.   
  25.     public String getDate() {  
  26.         return date;  
  27.     }  
  28.   
  29.     public void setDate(String date) {  
  30.         this.date = date;  
  31.     }  
  32.   
  33.     public String getDayPictureUrl() {  
  34.         return dayPictureUrl;  
  35.     }  
  36.   
  37.     public void setDayPictureUrl(String dayPictureUrl) {  
  38.         this.dayPictureUrl = dayPictureUrl;  
  39.     }  
  40.   
  41.     public String getNightPictureUrl() {  
  42.         return nightPictureUrl;  
  43.     }  
  44.   
  45.     public void setNightPictureUrl(String nightPictureUrl) {  
  46.         this.nightPictureUrl = nightPictureUrl;  
  47.     }  
  48.   
  49.     public String getWeather() {  
  50.         return weather;  
  51.     }  
  52.   
  53.     public void setWeather(String weather) {  
  54.         this.weather = weather;  
  55.     }  
  56.   
  57.     public String getWind() {  
  58.         return wind;  
  59.     }  
  60.   
  61.     public void setWind(String wind) {  
  62.         this.wind = wind;  
  63.     }  
  64.   
  65.     public String getTemperature() {  
  66.         return temperature;  
  67.     }  
  68.   
  69.     public void setTemperature(String temperature) {  
  70.         this.temperature = temperature;  
  71.     }  
  72.   
  73. }  

[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package com.lenovo.conference.entity.vo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  * 天气Bean 
  7.  *  
  8.  * @author SHANHY 
  9.  *  
  10.  */  
  11. @SuppressWarnings("serial")  
  12. public class WeatherBean_Baidu_City_Index implements Serializable {  
  13.   
  14.     private String title;//标题  
  15.     private String zs;//舒适度  
  16.     private String tipt;//指数简述  
  17.     private String des;//指数概述  
  18.   
  19.     public WeatherBean_Baidu_City_Index() {  
  20.         super();  
  21.     }  
  22.   
  23.     public String getTitle() {  
  24.         return title;  
  25.     }  
  26.   
  27.     public void setTitle(String title) {  
  28.         this.title = title;  
  29.     }  
  30.   
  31.     public String getZs() {  
  32.         return zs;  
  33.     }  
  34.   
  35.     public void setZs(String zs) {  
  36.         this.zs = zs;  
  37.     }  
  38.   
  39.     public String getTipt() {  
  40.         return tipt;  
  41.     }  
  42.   
  43.     public void setTipt(String tipt) {  
  44.         this.tipt = tipt;  
  45.     }  
  46.   
  47.     public String getDes() {  
  48.         return des;  
  49.     }  
  50.   
  51.     public void setDes(String des) {  
  52.         this.des = des;  
  53.     }  
  54.   
  55. }  

例子中解析所对应的JSON字符串

[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  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℃"}]}]} 
附上项目地址 http://download.csdn.net/detail/u011001084/9729342

这篇关于Java中Json字符串直接转换为对象(包括多层List集合 嵌套)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1006849

相关文章

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关