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":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"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℃"}]}]}


目录
相关文章
|
19天前
|
Java
Java快速入门之类、对象、方法
本文简要介绍了Java快速入门中的类、对象和方法。首先,解释了类和对象的概念,类是对象的抽象,对象是类的具体实例。接着,阐述了类的定义和组成,包括属性和行为,并展示了如何创建和使用对象。然后,讨论了成员变量与局部变量的区别,强调了封装的重要性,通过`private`关键字隐藏数据并提供`get/set`方法访问。最后,介绍了构造方法的定义和重载,以及标准类的制作规范,帮助初学者理解如何构建完整的Java类。
|
17天前
|
安全 Java
Object取值转java对象
通过本文的介绍,我们了解了几种将 `Object`类型转换为Java对象的方法,包括强制类型转换、使用 `instanceof`检查类型和泛型方法等。此外,还探讨了在集合、反射和序列化等常见场景中的应用。掌握这些方法和技巧,有助于编写更健壮和类型安全的Java代码。
30 17
|
1月前
|
Java
java代码优化:判断内聚到实体对象中和构造上下文对象传递参数
通过两个常见的java后端实例场景探讨代码优化,代码不是优化出来的,而是设计出来的,我们永远不可能有专门的时间去做代码优化,优化和设计在平时
31 15
|
2月前
|
JSON Java 数据格式
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
102 25
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
|
3月前
|
JSON Java 关系型数据库
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
在Java中,使用mybatis-plus更新实体类对象到mysql,其中一个字段对应数据库中json数据类型,更新时报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
300 4
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
|
3月前
|
安全 Java 编译器
Java对象一定分配在堆上吗?
本文探讨了Java对象的内存分配问题,重点介绍了JVM的逃逸分析技术及其优化策略。逃逸分析能判断对象是否会在作用域外被访问,从而决定对象是否需要分配到堆上。文章详细讲解了栈上分配、标量替换和同步消除三种优化策略,并通过示例代码说明了这些技术的应用场景。
Java对象一定分配在堆上吗?
|
3月前
|
存储 缓存 NoSQL
一篇搞懂!Java对象序列化与反序列化的底层逻辑
本文介绍了Java中的序列化与反序列化,包括基本概念、应用场景、实现方式及注意事项。序列化是将对象转换为字节流,便于存储和传输;反序列化则是将字节流还原为对象。文中详细讲解了实现序列化的步骤,以及常见的反序列化失败原因和最佳实践。通过实例和代码示例,帮助读者更好地理解和应用这一重要技术。
90 0
|
8月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1112 1
|
7月前
|
Java API Apache
怎么在在 Java 中对List进行分区
本文介绍了如何将列表拆分为给定大小的子列表。尽管标准Java集合API未直接支持此功能,但Guava和Apache Commons Collections提供了相关API。
|
7月前
|
运维 关系型数据库 Java
PolarDB产品使用问题之使用List或Range分区表时,Java代码是否需要进行改动
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。