使用するクラス:net.sf.json.jsonobject
JSONを使用して、JSON Webサイトにダウンロードされた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(jsonstrody); map <string、class> classmap = new hashmap = class>(); classmap.put( "results"、watherbean_baidu_city.class); classmap.put( "index"、watherbean_baidu_city_index.class); classmap.put( "Weather_data"、Weatherbean_baidu_city_weatherdata.class); // jsonをwatherbean_baidu weatherbean_baidu Weather =(watherbean_baidu)jsonobject.tobean(jsonobj、watherbean_baidu.class、classmap); System.out.println(wather.getResults());
使用されたいくつかのJavaクラスコード:
パッケージcom.lenovo.conference.entity.vo; import java.io.serializable; import java.util.list;/** * weather bean * * @author shanhy * */ @suppresswarnings( "serial")public class watherbean_baidu imprention datiableリスト<Weatherbean_baidu_city> results; //都市の天気予報セット(複数の都市を一度に照会できるため)public wathebean_baidu(){super();} public string getError(){return erser;} public void setError(string error){this.error = error;} public string getstatus {this.status = status;} public string getDate(){return date;} public void setdate(this.date = date;} public list <tearbean_baidu_city> getResults(){return result;} public void setResults(list <heathebean_city>} {thise.results}パッケージcom.lenovo.conference.entity.vo; import java.io.serializable; import java.util.list;/** * watherbean * * @author shanhy * */ @suppresswarnings( "serial")public class watherbean_baidu_city emprements serializableリスト<watherbean_baidu_city_index> index; // exponent set private list <weatherbean_city_weatherdata> weather_data; //数日間の天気コレクションpublic wathean_baidu_city {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(live bide_index() {this.index = index;} public list <weatherbean_baidu_city_weatherdata> getweather_data(){return weather_data;} public void setweather_data(list <weatherbean_baidu_city_weatherdata> heather_data){この.weather_data_data;}パッケージcom.lenovo.conference.entity.vo; import java.io.serializable;/** * weather bean * * @author shanhy * */ @suppresswarnings( "serial")public class_baidu_baidu_city_weatherdata実装{private string daypittureurl; NightPictureUrl; // Night Weather Private String Wind; // Wind Direction Private String温度; //温度Public Weatherbean_Baidu_City_WeatherData(){super();} public String getDate(){return date;} public void setDate(String date){this.date = date;} public String getDaypictureurl;} SetDayPictureUrl(String DayPictureUrl){this.daypictureurl = daypictureurl;} public string get nightpictureurl(){return nightpictureurl;} public nightpictureurl(string nightpictureurl){string nightpictureurl){this.nightpictureurl SetWeather(String Weather){this.weather = Weather;} public String getWind(){return wind;} public void setwind(this.wind = wind;} public string gettemperature(){return gettemperation;} public void setemperature(string heette){this.temperature = heert;}}}パッケージcom.lenovo.conference.entity.vo; Import java.io.serializable;/** * watherbean * * @author shanhy * */ @suppresswarnings( "serial")public class_baian_baidu_city_index emplentic概要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; 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"、 "result":["currentCity": "nanjing"、 "pm25": "83"、 "index":[{"withes": "chothes"、 "zs": "cooler"、 "wait": "人々はコート、ウールのコート、セーターを着用する必要があります。」}、{"タイトル"、「洗車」、「ZS」:「より適切な」、「洗車」:「デス」:「将来の雨が降ることはありません。 「}、{"title"、 "zs"、 "tipt": "Tipt": "Tourism Index"、 "des"は、気温がわずかに低くなり、少し涼しくなりますが、旅行にも適しています「Tipt」よりも:「コールドインデックス」、「DES」:「すべての気象条件が適切であり、明らかな冷却プロセスはありません。屋外の運動を主張する場合は、適切な運動を選択して暖かく保ちます。 "}、{" title ":" UV強度 "、" zs ":" wintest "、" tip ":" uv強度インデックス "、" des ":" winner uv強度指数 "、" des ":" uv放射線の天気が不要な理由。長い間屋外にいる場合は、SPFが8〜12の間に日焼け止めスキンケア製品を適用することをお勧めします。 "}]、" wather_data ":[{" dat e ":" 1月15日木曜日(リアルタイム: 6℃)、「daypictureurl ":" http://api.map.baidu.com/images/weather/day/yin.png "、" nightpictureurl ":" http://api.map.baidu.com/images/weather/night/night/night/duoyun.png "そよ風」、「温度」:「8〜 4℃ "}、{" date ":"金曜日 "、" daypictureurl ":" http://api.map.baidu.com/images/day/duoyun.png "、" nightpictureurl ":" http://api.map.baidu.comサニー」、「風」:「北西風のレベル3-4 "、「温度」:「12〜」 0℃ "}、{" date ":"土曜日 "、" daypictureurl ":" http://api.map.baidu.com/images/weather/day/qing.png "、" nightpictureurl ":" http://api.map.baidu.com/images/weather/night/night/night/ duun.png "曇り」、「風」:「北東風のレベル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/qing.png "、"天気 ":" xing "、" wind ":"西そよ風 "、"温度 ":" 10〜 -1℃ "}]}、{" currentCity ":" xuzhou "、" pm25 ":" 154 "、" index ":[" wittle ":" wathes "、" zs ":" cooler "、" tip ":" coldes index "、" des ":老人と弱い人は、コート、ウールコート、ウールのセーターを着用する必要があります。 "}、{" title ":" car Wash "、" zs ":"より適切な "、" Tip ":" car Wash index "、" des ":"車を洗うのに適しています。新しくスクラブされた車は、少なくとも1日は維持できます。 "}、{" title ":" Travel "、" zs ":"補足 "、" Tip ":" Tourism Index "、" des ":"天気は良くなりますが、旅行中の気分には影響しません。温度は適しており、そよ風が伴い、旅行に適しています。 "}、{" title ":" cold "、" zs ":" easy "、" tip ":"寒い気候が発生する可能性が高いので、服を適切に追加してください。弱い憲法を持つ友人は、保護に特別な注意を払う必要があります。 "}、{"タイトル ":「エクササイズ」、「zs」:「適切ではない」、「チップ」:「スポーツインデックス」、「デス」:「天気が良くなりますが、寒い気候を考えると、さまざまな屋内スポーツを行うことをお勧めします。屋外で運動する場合は、暖かく保ち、活動の準備に注意してください。 "}、{" title ":" UV強度 "、" zs ":" wintest "、" tip ":" uv強度指数 "、" des ":"これは弱いUV放射の天候であり、特別な保護は必要ありません。長い間屋外にいる場合は、SPFを備えた日焼け止めスキンケア製品を8〜12に適用することをお勧めします。 "}]、" Weather_data ":[{" date ":" 1月15日木曜日(リアルタイム: 6℃) "、" daypictureurl ":" http://api.map.baidu.com/images/weather/day/duoyun.png "、" nightpictureurl ":" http://api.map.baidu.com/images/weather/night/night/night/night/duoyun.png ""そよ風」、「温度」: "10〜 3℃ "}、{" date ":"金曜日 "、" daypictureurl ":" http://api.map.baidu.com/images/weather/day/duoyun.png "、" nightp ICTUREURL ":" http://api.map.baidu.com/images/weather/night/duoyun.png "、"天気 ":" Clandy "、" 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 "、" Weather ":" Clandy "、" Wind ":" East風風」、「温度」:「6〜」 -4℃ "}、{" date ":" Sunday "、" daypictureurl ":" http://api.map.baidu.com/images/weather/day/duoyun.png "、" nightpi ctureurl ":" http://api.map.baidu.com/images/weather/night/duoyun.png "、"天気 ":" Cloudy "、" Wind ":" Westerly風のレベル3-4 "、"温度 ":" 11〜 -1℃ "}]}]}]}JSON文字列をJava(マルチレイヤーリストコレクションを含む)のオブジェクトに変換する上記の方法は、私が共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。