사용 된 클래스 : 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 (jsonstrodge (jsonstbody); map <string, class> classmap = new Hashmap <string, class> (); classMap.put ( "results", weatherbean_baidu_city.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;/** * Weather Bean * * @author shanhy * */ @suppresswarnings ( "Serial") 공개 클래스 WeatherBean_Baidu는 SEALIALIND ORCELEAL; // State STRING STRINGS; // State STRING; 목록 <WeatherBean_Baidu_City> 결과; // 도시 날씨 예측 세트 (여러 도시가 한 번에 쿼리 될 수 있기 때문에) 공개 WeatherBean_Baidu () {super ();} public string getError () {return error;} public void seterror (문자열 오류) {this.error = error;} public setstatus (reprations status) {this.status = status;} public string getDate () {return date;} public void setDate (문자열 날짜) {this.date = date;} public list <weatherbean_baidu_city> getResults () {return recults;} public void setResults (list <weatherbean_baidu_city> results) package com.lenovo.conference.entity.vo; import java.io.serializable; import java.util.list;/** * weatherbean * * @author shanhy * */ @suppresswarnings ( "Serial") 공개 클래스 WeatherBean_baidu_city Serializable; // city string pm25; 목록 <WeatherBean_baidu_city_index> index; // 지수 세트 비공개 목록 세트 <WeatherBean_Baidu_City_WeatherData> Weather_Data; // 며칠의 날씨 컬렉션 공개 weatherbean_baidu_city () {super ();} 공개 문자열 getCurrentCity () {return currentCity;} public void setcity (string curittcity) {threat void setcity = currentCity;} public string getpm25 () {return pm25;} public void setpm25 (문자열 pm25) {this.pm25 = pm25;} public list <weatherbean_baidu_city_index> getIndex () {return void setindex (listbean_baidu_city_city_city_index). index;} 공개 목록 <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") 공개 클래스 WeatherBean_baidu_city_weatherdata serializable {private string daypictureurl; // day privity string; // daypicture arl; 날씨 개인 문자열 바람; // 바람 방향 개인 문자열 온도; // 온도 공개 weatherbean_baidu_city_weatherdata () {super ();} public String getDate () {return date;} public void setDate (문자열 날짜) {this.date =} public string getDayPictureUl () void void setDayPictureUl; dayPictureUrl) {this.daypictureUrl = dayPictureUrl;} public String getnightPictureUl () {return nightPictureUrl;} public void setnightpictureUrl (String nightPictureUrl) {this.nightPictureUrl = nightPictureUrl; {this.weather = weather;} public string getwind () {return wind;} public void setwind (String Wind) {this.wind = wind;} public String getTemperature () {return retless;} public void settemperature (문자열 온도) {this.temperature = 온도;}} package com.lenovo.conference.entity.vo; import java.io.serializable;/** * weatherbean * * @author shanhy * */ @suppresswarnings ( "Serial") 공개 클래스 WeatherBean_baidu_city_index는 Serializable {private string zs; // private string ZS; // private string; // private string; // private string; // 개요 공개 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) {}}}}}}}} void settipt (String tip) {this.tip = tip;} public String getDes () {return des;} public void setdes (String des) {this.des = des;}}이 예에서는 해당 JSON 문자열을 구문 분석하십시오
{ "error": 0, "status": "success", "date": "2015-01-15", "결과": [{ "currentcity": "nanjing", "83", "index": [{ "제목": "의류": "냉각기", "weal index": "weal weal" 사람들은 코트, 양모 코트 및 스웨터를 착용해야합니다. "}, {"Title ":"Car Wash ","ZS ":"더 적합한 ","Tipt ":"Car Wash Index ","Des ":"미래에는 비가 내릴 수 없으며 바람이 적어도 하루 동안 지속될 수 있습니다. "}, {title": "travel", "zs": "공급": "관광", "Tipt": "Tourism Index", "Des": "온도는 약간 낮으며 약간 시원하지만 여행에 적합하지만 {" "" "". " ","Tipt ":"Cold Index ","des ":"모든 기상 조건은 적합하며, 냉각 과정이 명백하지 않으며 감기의 확률은 낮습니다. "},"연습 ":"ZS ":"팁 ","운동 ","Des ":"Cloudy and Colding " 야외 운동을 고집하면 올바른 운동을 선택하고 따뜻하게 유지하십시오. "}, {"title ":"UV 강도 ","ZS ":"wintest ","팁 ":"UV 강도 색인 ","des ":"우승자 UV 강도 색인 ","DES ":"UV 방사 날씨가 필요하지 않은 이유 ":" 오랫동안 야외에서 SPF가있는 선 스크린 스킨 케어 제품을 적용하는 것이 좋습니다. "}],"weather_data ": [{"dat e ":"1 월 15 일 목요일 : 실시간 : "DayPictureUrl": "http://api.map.baidu.com/images/weather/day/yin.png", "nightpictureurl": "http://api.map.baidu.com/images/weather/night/duoyun.png" 바람 ","온도 ":"8 ~ 4 "}, {"date ":"friday ","daypictureUrl ":"http://api.map.baidu.com/images/weather/day/duoyun.png ","NightpictureUrl ":"http://api.map.baidu.com/weather/wnather/night/quing.png ":": ":": ":": ":": " Sunny ","Wind ":"북서쪽 바람 수준 3-4 ","온도 ":"12 ~ 0 "}, {"date ":"토요일 ","daypictureUrl ":"http://api.map.baidu.com/images/weather/day/qing.png ","nightpictureUrl ":"http://api.map.baidu.com/images/weather/weather/wherton.png ",": ":": ":"weather ":"s " 흐린 ","바람 ":"북동풍 3-4 ","온도 ":"8 ~ 0 "}, {"date ":"sunday ","daypictureurl ":"http://api.map.baidu.com/images/weather/day/qing.png ","nightp ictureurl ":"http://api.map.baidu.com/images/weather/night/quing.png ","날씨 ":"Xing ","Wind ":"Western Breeze ","온도 ":"10 ~ -1 ℃ "}]}, {"CurrentCity ":"Xuzhou ","PM25 ":"154 ","Index ": [{"title ":"의류 ","ZS ":"팁 ":"옷 색인 ","Dest ":"두꺼운 코트 및 기타 의류. 노인과 약한 사람들은 코트, 양모 코트 및 양모 스웨터를 착용해야합니다. "}, {"title ":"Car Wash ","ZS ":"더 적합한 ","팁 ":"세차인 지수 ","des ":"세탁에 더 적합하면 향후 비나 적은 풍력이 없을 것입니다. 새로 문지른 차는 적어도 하루 동안 유지할 수 있습니다. "}, {"title ":"travel ","zs ":"보충 ","팁 ":"관광 지수 ","des ":"날씨가 더 좋지만 여행 할 때는 기분에 영향을 미치지 않습니다. 온도가 적합하고 산들 바람이 동반되어 여행에 적합합니다. "}, {"Title ":"Cold ","ZS ":"Easy ","Tip ":"추운 날씨가 발생할 가능성이 높으므로 옷을 적절하게 추가하십시오. 헌법이 약한 친구는 보호에 특별한주의를 기울여야합니다. "}, {"title ":"운동 ","ZS ":"적합하지 않음 ","팁 ":"스포츠 인덱스 ","des ":"날씨가 더 좋지만 추운 날씨를 고려하면 다양한 실내 스포츠를 수행하는 것이 좋습니다. 야외에서 운동하는 경우 따뜻하게 유지하고 활동 준비에주의를 기울이십시오. "}, {"title ":"UV 강도 ","ZS ":"wintest ","팁 ":"UV 강도 색인 ","des ":"그것은 약한 UV 방사선 날씨이며 특별한 보호가 필요하지 않습니다. 오랫동안 야외에있는 경우 8-12 사이의 SPF가있는 선 스크린 스킨 케어 제품을 적용하는 것이 좋습니다. "}],"weather_data ": [{"date ":"1 월 15 일 목요일 (실시간 : 6 day) ","daypictureUrl ":"http://api.map.baidu.com/images/weather/dadun.png ","nightpictureurl ":"http://api.map.baidu.com/images/weather/night/duoyun.png " 바람 ","온도 ":"10 ~ 3 "}, {"Date ":"Friday ","DayPictureUrl ":"http://api.map.baidu.com/images/weather/day/duoyun.png ","Nightp ictureurl ":"http://api.map.baidu.com/images/weather/night/duoyun.png ","날씨 ":"Cloudy ","Wind ":"North 바람 수준 3-4 ","온도 ":"11 ~ -4 ℃ "}, {"date ":"토요일 ","daypictureUrl ":"http://api.map.baidu.com/images/weather/day/duoyun.png ","nigh tpictureUrl ":"http://api.map.baidu.com/images/weather/night/duoyun.png ","날씨 ":"Cloudy ","Wind ":"East 바람 바람 ","온도 ":"6 ~ -4 ℃ "}, {"date ":"일요일 ","daypictureUrl ":"http://api.map.baidu.com/images/weather/day/duoyun.png ","Nightpi cteurl ":"http://api.map.baidu.com/images/weather/night/duoyun.png ","날씨 ":"Cloudy ","Wind ":"Westerly 바람 수준 3-4 ","온도 ":"11 ~ -1 ℃ "}}}}]}JSON 문자열을 Java의 객체로 변환하는 위의 방법 (다층 목록 컬렉션 포함)은 내가 공유하는 모든 컨텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.