Java中Json字符串直接转换为对象的方法(包括多层List集合)
程序员文章站
2024-03-13 21:08:10
使用到的类:net.sf.json.jsonobject
使用json时,除了要导入json网站上面下载的json-lib-2.2-jdk15.jar包之外,...
使用到的类: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集合)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。