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℃"}]}]}

以上这篇Java中Json字符串直接转换为对象的方法(包括多层List集合)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 使用GSON库将Java中的map键值对应结构对象转换为JSON

    Map的存储结构式Key/Value形式,Key 和 Value可以是普通类型,也可以是自己写的JavaBean(本文),还可以是带有泛型的List. (GSON的GitHub项目页:https://github.com/google/gson) JavaBean 本例中您要重点看如何将Json转回为普通JavaBean对象时TypeToken的定义. 实体类: public class Point { private int x; private int y; public Point(int

  • java 和 json 对象间转换

    1. json-lib是一个java类库,提供将Java对象,包括beans, maps, collections, java arrays and XML等转换成JSON,或者反向转换的功能. 2. json-lib 主页 : http://json-lib.sourceforge.net/ 3.执行环境 需要以下类库支持 commons-lang 2.5commons-beanutils 1.8.0commons-collections 3.2.1commons-logging 1.1.1e

  • java对象与json对象之间互相转换实现方法示例

    本文实例讲述了java对象与json对象之间互相转换实现方法.分享给大家供大家参考,具体如下: import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class MainClass { public st

  • 使用Jackson来实现Java对象与JSON的相互转换的教程

    一.入门 Jackson中有个ObjectMapper类很是实用,用于Java对象与JSON的互换. 1.JAVA对象转JSON[JSON序列化] import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonDemo { p

  • JSON数据转换成Java对象的方法

    第一种方法,使用 JSON-lib .第二种方法,使用 JACKSON.前两种方法,对相对简单的Pojo 对象来说,还是比较容易的.但是相对于嵌套多层的数据来说,复杂度就直接上去了.第三种方法,使用GOOGLE 的Gson 来解决了.写过安卓的都知道,这东西,是Google出来的,最大的好处就是,基本不依赖其他的包.用起来自然很爽,取值方式非常灵活.对复杂的JSON 取值,基本统统搞定.在Gson 中分为两种概念.一个就是 JsonObject 和 JsonArray.具体的看代码 复制代码 代

  • GSON实现Java对象与JSON格式对象相互转换的完全教程

    Gson是一个Java库,用来实现Json和Java对象之间的相互转换.Gson是一个托管在https://github.com/google/gson的开源项目. Gson中主要的类是Gson,也可以使用类GsonBuilder在创建Gson对象的同时设置一些选项. Gson对象在处理Json时不会保存任何状态,所以使用者能够很轻松的对同一个Gson对象进行多次序列化.反序列化等操作. 示例:基本使用 //Serialization Gson gson = new Gson(); gson.t

  • 使用GSON库转换Java对象为JSON对象的进阶实例详解

    对List和map等结构的常用转换操作基本上可以满足我们处理的绝大多数需求,但有时项目中对json有特殊的格式规定.比如下面的json串解析: [{"tableName":"students","tableData":[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:54:49 PM"},{"

  • json转换成java对象示例

    json字符串转Java对象有很多工具可以使用,下面的小例子只是我练手的 复制代码 代码如下: import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import com.jfinal.kit.JsonKit; public class JsonToJavaObject { public static void main(String[] args) {  O

  • JAVA对象JSON数据互相转换的四种常见情况

    1. 把java 对象列表转换为json对象数组,并转为字符串 复制代码 代码如下: JSONArray array = JSONArray.fromObject(userlist);     String jsonstr = array.toString(); 2.把java对象转换成json对象,并转化为字符串 复制代码 代码如下: JSONObject object = JSONObject.fromObject(invite);    String str=object.toString

  • java对象与json对象间的相互转换的方法

    工程中所需的jar包,因为在网上不太好找,所以我将它放到我的网盘里了,如有需要随便下载. 点击下载 1.简单的解析json字符串 首先将json字符串转换为json对象,然后再解析json对象,过程如下. JSONObject jsonObject = JSONObject.fromObject(jsonStr); 根据json中的键得到它的值 String name = jsonObject.getString("name"); int num = jsonObject.getInt

  • JSON的String字符串与Java的List列表对象的相互转换

    在前端: 1.如果json是List对象转换的,可以直接遍历json,读取数据. 2.如果是需要把前端的List对象转换为json传到后台,param是ajax的参数,那么转换如下所示: var jsonStr = JSON.stringify(list); var param= {}; param.jsonStr=jsonStr; 在后台: 1.把String转换为List(str转换为list) List<T> list = new ArrayList<T>(); JSONAr

随机推荐