Class used: net.sf.json.JSONObject
When using JSON, in addition to importing the json-lib-2.2-jdk15.jar package downloaded on the JSON website, there must be several other dependencies: commons-beanutils.jar, commons-httpclient.jar, commons-lang.jar, ezmorph.jar, morph-1.0.1.jar
Here is the example code:
// JSON conversion 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); // Convert JSON to WeatherBean_Baidu WeatherBean_Baidu weather = (WeatherBean_Baidu) JSONObject.toBean(jsonObj, WeatherBean_Baidu.class, classMap); System.out.println(weather.getResults());Several JAVA class codes used:
package com.lenovo.conference.entity.vo;import java.io.Serializable;import java.util.List;/** * Weather Bean * * @author SHANHY * */@SuppressWarnings("serial")public class WeatherBean_Baidu implements Serializable {private String error;//Error number private String status;//State value private String date;//Date private List<WeatherBean_Baidu_City> results;//City weather forecast set (because multiple cities can be queried at once) 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;/** * WeatherBean * * @author SHANHY * */@SuppressWarnings("serial")public class WeatherBean_Baidu_City implements Serializable {private String currentCity;//City name private String pm25;//pm2.5 value private List<WeatherBean_Baidu_City_Index> index;//Exponent set private List<WeatherBean_Baidu_City_Weatherdata> weather_data;//Weather collection of several days 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;/** * Weather Bean * * @author SHANHY * */@SuppressWarnings("serial")public class WeatherBean_Baidu_City_Weatherdata implements Serializable {private String date;// Date private String dayPictureUrl;// Day weather private String nightPictureUrl;// Night weather private String wind;// Wind direction private String temperature;// 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;/** * WeatherBean * * @author SHANHY * */@SuppressWarnings("serial")public class WeatherBean_Baidu_City_Index implements Serializable {private String title;//Title private String zs;//Comfort private String tip;//Index brief description private String des;//Index overview 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 tip;}public void setTipt(String tip) {this.tip = tip;}public String getDes() {return des;}public void setDes(String des) {this.des = des;}}In the example, parse the corresponding JSON string
{"error":0,"status":"success","date":"2015-01-15","results":[{"currentCity":"Nanjing","pm25":"83","index":[{"title":"Clothes","zs":"Cooler","tipt":"Clothes index","des":"It is recommended to wear thick coats and sweaters. Old and weak people should wear coats, wool coats and sweaters."},{"title":"Car wash","zs":"More suitable","tipt":" Car wash index","des":"More suitable for washing cars, there will be no rain in the future and the wind is less. Scrubbing a new car can last for at least one day. "},{"title":"Travel","zs":"Supply","tipt":"Tourism","tipt":"Tourism index","des":"The weather is better, the temperature is slightly lower, and it will feel a little cool, but it is also a good weather. It is suitable for travel, but don't miss the opportunity! "},{"title":"Cold","zs":"Less than","tipt":"Cold index","des":"All weather conditions are suitable, there is no obvious cooling process, and the probability of a cold is low. "},{ "title":"Exercise","zs":"Not suitable","tip":"Exercise index","des":"On cloudy and cold weather, it is recommended that you perform low-intensity exercise indoors; if you insist on outdoor exercise, please choose the right exercise and keep warm. "},{"title":"UV intensity","zs":"Wintest","tip":"UV intensity index","des":"Winner UV intensity index","des":"Why UV radiation weather is not required. If you are outdoors for a long time, it is recommended to apply sunscreen skin care products with SPF between 8 and 12."}],"weather_data":[{"dat e":"Thursday January 15 (Real Time: 6℃),"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"cloud to cloudy","wind":"Northern breeze","temperature":"8 ~ 4℃"},{"date":"Friday","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"cloud to sunny","wind":"Northwest wind level 3-4","temperature":"12 ~ 0℃"},{"date":"Saturday","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"Sunny to cloudy","wind":"Northeast wind level 3-4","temperature":"8 ~ 0℃"},{"date":"Sunday","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"Xing","wind":"Western breeze","temperature":"10 ~ -1℃"}]},{"currentCity":"Xuzhou","pm25":"154","index":[{"title":"Clothes","zs":"Cooler","tip":"Clothes index","des":"It is recommended to wear thick coats, sweaters and other clothing. Old and weak people should wear coats, wool coats and wool sweaters. "},{"title":"Car wash","zs":"More suitable","tip":"Car wash index","des":"More suitable for washing cars, there will be no rain or less wind power in the future. A newly scrubbed car can be maintained for at least one day. "},{"title":"Travel","zs":"Supplementary","tip":"Tourism index","des":"The weather is better, but it will not affect your mood when traveling. The temperature is suitable and the breeze is accompanied by, making it suitable for travel. "},{"title":"cold","zs":"easy","tip":"cold weather is more likely to occur, so please add clothes appropriately. Friends with weak constitutions should pay special attention to protection. "},{"title":"Exercise","zs":"Not suitable","tip":"Sport index","des":"The weather is better, but considering the cold weather, it is recommended that you perform various indoor sports. If you exercise outdoors, please pay attention to keeping warm and preparing for activities. "},{"title":"UV intensity","zs":"Wintest","tip":"UV intensity index","des":"It is a weak UV radiation weather, and no special protection is required. If you are outdoors for a long time, it is recommended to apply sunscreen skin care products with SPF between 8-12. "}],"weather_data":[{"date":"Thursday January 15 (Real Time: 6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"cloudy","wind":"South breeze","temperature":"10 ~ 3℃"},{"date":"Friday","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"cloudy","wind":"North Wind Level 3-4","temperature":"11 ~ -4℃"},{"date":"Saturday","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"cloudy","wind":"east wind breeze","temperature":"6 ~ -4℃"},{"date":"Sunday","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"cloudy","wind":"westerly wind level 3-4","temperature":"11 ~ -1℃"}]}]}]}The above method of converting Json strings into objects in Java (including multi-layer List collections) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.