Java中Json字符串直接转换为对象的方法(包括多层List集合)
作者:jingxian 发布时间:2021-06-26 18:13:42
标签: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":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"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℃"}]}]}


猜你喜欢
- 需求使用 spring-boot 项目开发中,项目启动时“非常”慢的。如果每次修改代码或静态资源文件后都需要重新启动项目,这是多么痛苦的事。
- 我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用。而ViewPage
- 1、static关键字1.1 使用static关键字定义属性在讲解static定义属性操作之前,首先编写如下一道程序。现在定义一个表示中国人
- Android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:OOM;处理策略:1.使用缩略图(Thumbnail
- Android Material Design:PopupMenuAndroid Material Design 引入的PopupMenu类
- mybatis-plus 3.0.1 枚举返回为null解决办法2020-11-02 14:28:48今天再次回到代码里无意间看到,原来和m
- 经常有同学问到,使用Android能不能开发游戏呢?能开发那些游戏呢?由于操作系统和开发语言局限,一般开发安卓手机游戏,我们很少使用其自带语
- 1、概述本文通过手动实现迭代器来了解foreach语句的本质。2、使用foreach语句遍历集合在C#中,使用foreach语句来遍历集合。
- 用java的框架和面板的知识做的一个展示月食过程的小程序。这里的想法就是先把背景设置成黑色,然后画一个黄色的圆作为月亮,接着画一个黑色的圆,
- Java的NIO中的管道,就类似于实际中的管道,有两端,一段作为输入,一段作为输出。也就是说,在创建了一个管道后,既可以对管道进行写,也可以
- 前言记得前段时间的文章么?redis使用位图法记录在线用户的状态,还是需要自己实现一个IM在线用户状态的记录,今天来讲讲另一方案,布隆过滤器
- 最近把以前制作的截图程序重新写了一下动了一个大手术 高质量仿照的TX的截图程序先看几个效果图拖动过程中显示当前鼠标下一小块的图像信息 尺寸、
- 有这么一种应用场景:你是做前端或APP开发的,需要调用服务端提供的接口,接口只能在公司内网访问;在公司外就无法调试代码了。想在公司外访问怎么
- 本文实例讲述了Java泛型与数据库应用。分享给大家供大家参考,具体如下:一 点睛BaseDao定义了基本的数据库增删查改, 之后可以继承该泛
- 本文实例为大家分享了java实现简单石头剪刀布游戏的具体代码,供大家参考,具体内容如下问题描述Alice, Bob和Cindy一起玩猜拳的游
- 废话不多说了,具体代码如下所述:staticvoid Main(string[] args){ st
- 在项目推进中,如果说第一件事是搭Spring框架的话,那么第二件事情就是在Sring基础上搭建日志框架,我想很多人都知道日志对于一个项目的重
- 一、基本使用1、准备工程和引入控件1、下载、安装FastReport这一步很简单,大家在其中文网站上下载最新版的demo版就可以了,直接安装
- 延迟队列延迟队列存储的对象肯定是对应的延时消息,所谓”延时消息”是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者
- 异常的英文单词是exception,字面翻译就是“意外、例外”的意思,也就是非正常情况。事实上,异常本质上是程序上的错误,包括程序逻辑错误和