多层json字符串转map_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 classMap = new HashMap();
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 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;
}
json转换对象public List getResults() {
return results;
}
public void setResults(List results) { sults = 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 index;//指数集合
private List 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 getIndex() {
return index;
}
public void setIndex(List index) {
this.index = index;
}
public List getWeather_data() {
return weather_data;
}
public void setWeather_data(
List weather_data) {
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 {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。