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) Bean(jsonObj,
WeatherBean_Baidu.class, classMap);
System.out.Results());
使⽤到的⼏个JAVA类代码:
package ity.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) {
< = 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) {
}
}
package ity.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 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) {json转换对象
this.weather_data = weather_data;
}
}
package ity.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) {
}
}
package ity.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 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":"洗车以上这篇Java中Json字符串直接转换为对象的⽅法(包括多层List集合)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论