如何从JSON回复中有意义地提取信息?

回复印度孟买

{ "response": { "version": "0.1" "termsofService": "http://www.wunderground.com/weather/api/d/terms.html" "features": { "geolookup": 1 "conditions": 1 "forecast": 1 } } "location": { "type":"INTLCITY" "country":"IN" "country_iso3166":"IN" "country_name":"India" "state":"" "city":"Mumbai" "tz_short":"IST" "tz_long":"Asia/Kolkata" "lat":"19.12000084" "lon":"72.84999847" "zip":"00000" "magic":"1" "wmo":"43003" "l":"/q/zmw:00000.1.43003" "requesturl":"global/stations/43003.html" "wuiurl":"http://www.wunderground.com/global/stations/43003.html" "nearby_weather_stations": { "airport": { "station": [ { "city":"Mumbai / Santacruz" "state":"" "country":"India" "icao":"VABB" "lat":"19.11666679" "lon":"72.84999847" } { "city":"Mumbai" "state":"" "country":"IN" "icao":"VABB" "lat":"19.12000084" "lon":"72.84999847" } ] } "pws": { "station": [ ] } } } "current_observation": { "image": { "url":"http://sofzh.miximages.com/java/logo_130x80.png" "title":"Weather Underground" "link":"http://www.wunderground.com" } "display_location": { "full":"Mumbai India" "city":"Mumbai" "state":"" "state_name":"India" "country":"IN" "country_iso3166":"IN" "zip":"00000" "latitude":"19.12000084" "longitude":"72.84999847" "elevation":"14.00000000" } "observation_location": { "full":"Mumbai " "city":"Mumbai" "state":"" "country":"IN" "country_iso3166":"IN" "latitude":"19.12000084" "longitude":"72.84999847" "elevation":"46 ft" } "estimated": { } "station_id":"VABB" "observation_time":"Last Updated on February 16 5:10 PM IST" "observation_time_rfc822":"Sat 16 Feb 2013 17:10:00 +0530" "observation_epoch":"1361014800" "local_time_rfc822":"Sat 16 Feb 2013 17:25:20 +0530" "local_epoch":"1361015720" "local_tz_short":"IST" "local_tz_long":"Asia/Kolkata" "local_tz_offset":"+0530" "weather":"Clear" "temperature_string":"77 F (25 C)" "temp_f":77 "temp_c":25 "relative_humidity":"54%" "wind_string":"From the WNW at 8 MPH" "wind_dir":"WNW" "wind_degrees":290 "wind_mph":8 "wind_gust_mph":0 "wind_kph":13 "wind_gust_kph":0 "pressure_mb":"1008" "pressure_in":"29.77" "pressure_trend":"0" "dewpoint_string":"59 F (15 C)" "dewpoint_f":59 "dewpoint_c":15 "heat_index_string":"NA" "heat_index_f":"NA" "heat_index_c":"NA" "windchill_string":"NA" "windchill_f":"NA" "windchill_c":"NA" "feelslike_string":"77 F (25 C)" "feelslike_f":"77" "feelslike_c":"25" "visibility_mi":"3.7" "visibility_km":"6.0" "solarradiation":"" "UV":"-1" "precip_1hr_string":"-9999.00 in (-9999.00 mm)" "precip_1hr_in":"-9999.00" "precip_1hr_metric":"-9999.00" "precip_today_string":"0.00 in (0.0 mm)" "precip_today_in":"0.00" "precip_today_metric":"0.0" "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "forecast_url":"http://www.wunderground.com/global/stations/43003.html" "history_url":"http://www.wunderground.com/history/airport/VABB/2013/2/16/DailyHistory.html" "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=19.12000084 72.84999847" } "forecast":{ "txt_forecast": { "date":"5:30 AM IST" "forecastday": [ { "period":0 "icon":"chancerain" "icon_url":"http://sofzh.miximages.com/java/chancerain.gif" "title":"Saturday" "fcttext":"Partly cloudy with a chance of rain in the morning then clear. High of 81F. Winds from the WNW at 10 to 15 mph. Chance of rain 30%." "fcttext_metric":"Partly cloudy with a chance of rain in the morning then clear. High of 27C. Breezy. Winds from the WNW at 15 to 25 km/h. Chance of rain 30%." "pop":"30" } { "period":1 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Saturday Night" "fcttext":"Clear. Low of 68F. Winds less than 5 mph." "fcttext_metric":"Clear. Low of 20C. Winds less than 5 km/h." "pop":"0" } { "period":2 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Sunday" "fcttext":"Clear. High of 82F. Winds from the NE at 5 to 15 mph shifting to the WNW in the afternoon." "fcttext_metric":"Clear. High of 28C. Winds from the NE at 5 to 20 km/h shifting to the WNW in the afternoon." "pop":"0" } { "period":3 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Sunday Night" "fcttext":"Clear. Low of 66F. Winds from the NNE at 5 to 10 mph." "fcttext_metric":"Clear. Low of 19C. Winds from the NNE at 10 to 15 km/h." "pop":"0" } { "period":4 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Monday" "fcttext":"Clear. High of 88F. Winds from the NNE at 5 to 15 mph shifting to the NW in the afternoon." "fcttext_metric":"Clear. High of 31C. Breezy. Winds from the NNE at 10 to 20 km/h shifting to the NW in the afternoon." "pop":"0" } { "period":5 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Monday Night" "fcttext":"Clear. Low of 63F. Winds from the NNE at 5 to 10 mph." "fcttext_metric":"Clear. Low of 17C. Winds from the NNE at 5 to 15 km/h." "pop":"0" } { "period":6 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Tuesday" "fcttext":"Clear. High of 90F. Winds from the North at 5 to 10 mph shifting to the WNW in the afternoon." "fcttext_metric":"Clear. High of 32C. Winds from the North at 5 to 20 km/h shifting to the WNW in the afternoon." "pop":"0" } { "period":7 "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "title":"Tuesday Night" "fcttext":"Clear. Low of 72F. Winds from the North at 5 to 10 mph." "fcttext_metric":"Clear. Low of 22C. Winds from the North at 5 to 15 km/h." "pop":"0" } ] } "simpleforecast": { "forecastday": [ {"date":{ "epoch":"1361037600" "pretty":"11:30 PM IST on February 16 2013" "day":16 "month":2 "year":2013 "yday":46 "hour":23 "min":"30" "sec":0 "isdst":"0" "monthname":"February" "weekday_short":"Sat" "weekday":"Saturday" "ampm":"PM" "tz_short":"IST" "tz_long":"Asia/Kolkata"} "period":1 "high": { "fahrenheit":"81" "celsius":"27" } "low": { "fahrenheit":"68" "celsius":"20" } "conditions":"Chance of Rain" "icon":"chancerain" "icon_url":"http://sofzh.miximages.com/java/chancerain.gif" "skyicon":"mostlysunny" "pop":30 "qpf_allday": { "in": 0.01 "mm": 0.3 } "qpf_day": { "in": 0.00 "mm": 0.0 } "qpf_night": { "in": 0.00 "mm": 0.0 } "snow_allday": { "in": 0 "cm": 0 } "snow_day": { "in": 0 "cm": 0 } "snow_night": { "in": 0 "cm": 0 } "maxwind": { "mph": 14 "kph": 22 "dir": "WNW" "degrees": 294 } "avewind": { "mph": 12 "kph": 19 "dir": "WNW" "degrees": 295 } "avehumidity": 53 "maxhumidity": 72 "minhumidity": 49 } {"date":{ "epoch":"1361124000" "pretty":"11:30 PM IST on February 17 2013" "day":17 "month":2 "year":2013 "yday":47 "hour":23 "min":"30" "sec":0 "isdst":"0" "monthname":"February" "weekday_short":"Sun" "weekday":"Sunday" "ampm":"PM" "tz_short":"IST" "tz_long":"Asia/Kolkata"} "period":2 "high": { "fahrenheit":"82" "celsius":"28" } "low": { "fahrenheit":"66" "celsius":"19" } "conditions":"Clear" "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "skyicon":"sunny" "pop":0 "qpf_allday": { "in": 0.00 "mm": 0.0 } "qpf_day": { "in": 0.00 "mm": 0.0 } "qpf_night": { "in": 0.00 "mm": 0.0 } "snow_allday": { "in": 0 "cm": 0 } "snow_day": { "in": 0 "cm": 0 } "snow_night": { "in": 0 "cm": 0 } "maxwind": { "mph": 11 "kph": 18 "dir": "NW" "degrees": 306 } "avewind": { "mph": 8 "kph": 13 "dir": "WSW" "degrees": 238 } "avehumidity": 55 "maxhumidity": 64 "minhumidity": 39 } {"date":{ "epoch":"1361210400" "pretty":"11:30 PM IST on February 18 2013" "day":18 "month":2 "year":2013 "yday":48 "hour":23 "min":"30" "sec":0 "isdst":"0" "monthname":"February" "weekday_short":"Mon" "weekday":"Monday" "ampm":"PM" "tz_short":"IST" "tz_long":"Asia/Kolkata"} "period":3 "high": { "fahrenheit":"88" "celsius":"31" } "low": { "fahrenheit":"63" "celsius":"17" } "conditions":"Clear" "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "skyicon":"sunny" "pop":0 "qpf_allday": { "in": 0.00 "mm": 0.0 } "qpf_day": { "in": 0.00 "mm": 0.0 } "qpf_night": { "in": 0.00 "mm": 0.0 } "snow_allday": { "in": 0 "cm": 0 } "snow_day": { "in": 0 "cm": 0 } "snow_night": { "in": 0 "cm": 0 } "maxwind": { "mph": 12 "kph": 19 "dir": "NW" "degrees": 310 } "avewind": { "mph": 9 "kph": 14 "dir": "SW" "degrees": 235 } "avehumidity": 44 "maxhumidity": 62 "minhumidity": 30 } {"date":{ "epoch":"1361296800" "pretty":"11:30 PM IST on February 19 2013" "day":19 "month":2 "year":2013 "yday":49 "hour":23 "min":"30" "sec":0 "isdst":"0" "monthname":"February" "weekday_short":"Tue" "weekday":"Tuesday" "ampm":"PM" "tz_short":"IST" "tz_long":"Asia/Kolkata"} "period":4 "high": { "fahrenheit":"90" "celsius":"32" } "low": { "fahrenheit":"72" "celsius":"22" } "conditions":"Clear" "icon":"clear" "icon_url":"http://sofzh.miximages.com/java/clear.gif" "skyicon":"sunny" "pop":0 "qpf_allday": { "in": 0.00 "mm": 0.0 } "qpf_day": { "in": 0.00 "mm": 0.0 } "qpf_night": { "in": 0.00 "mm": 0.0 } "snow_allday": { "in": 0 "cm": 0 } "snow_day": { "in": 0 "cm": 0 } "snow_night": { "in": 0 "cm": 0 } "maxwind": { "mph": 10 "kph": 16 "dir": "WNW" "degrees": 302 } "avewind": { "mph": 8 "kph": 13 "dir": "West" "degrees": 272 } "avehumidity": 46 "maxhumidity": 51 "minhumidity": 30 } ] } }} --------------------------- mph: 8, 

回复澳大利亚悉尼

 { "response": { "version": "0.1" "termsofService": "http://www.wunderground.com/weather/api/d/terms.html" "features": { "geolookup": 1 "conditions": 1 "forecast": 1 } } "location": { "type":"INTLCITY" "country":"AU" "country_iso3166":"AU" "country_name":"Australia" "state":"NW" "city":"Sydney" "tz_short":"EST" "tz_long":"Australia/Sydney" "lat":"-33.95000076" "lon":"151.17999268" "zip":"00000" "magic":"1" "wmo":"94767" "l":"/q/zmw:00000.1.94767" "requesturl":"global/stations/94767.html" "wuiurl":"http://www.wunderground.com/global/stations/94767.html" "nearby_weather_stations": { "airport": { "station": [ { "city":"Sydney Airport" "state":"" "country":"Australia" "icao":"YSSY" "lat":"-33.95000076" "lon":"151.18333435" } { "city":"Sydney" "state":"" "country":"AU" "icao":"YSSY" "lat":"-33.95000076" "lon":"151.17999268" } { "city":"Richmond (NSW)" "state":"" "country":"AU" "icao":"YSRI" "lat":"-33.59999847" "lon":"150.77999878" } ] } "pws": { "station": [ { "neighborhood":"Matraville" "city":"Sydney" "state":"NSW" "country":"AUSTRALIA" "id":"INSWMATR2" "lat":-33.957550 "lon":151.230850 "distance_km":4 "distance_mi":2 } { "neighborhood":"Bardwell Park" "city":"Sydney" "state":"New South Wales" "country":"AUSTRALIA" "id":"INEWSOUT37" "lat":-33.938381 "lon":151.124359 "distance_km":5 "distance_mi":3 } { "neighborhood":"APRSWXNET Sydney AU" "city":"Little Bay" "state":"NW" "country":"AU" "id":"MD5399" "lat":-33.958500 "lon":151.240677 "distance_km":5 "distance_mi":3 } { "neighborhood":"" "city":"Pyrmont" "state":"NEW SOUTH WALES" "country":"AUSTRALIA" "id":"INEWSOUT155" "lat":-33.871807 "lon":151.194977 "distance_km":8 "distance_mi":5 } { "neighborhood":"Woolloomooloo" "city":"Woolloomooloo" "state":"NSW" "country":"AUSTRALIA" "id":"INSWWOOL3" "lat":-33.874527 "lon":151.220734 "distance_km":9 "distance_mi":5 } { "neighborhood":"Old\u0027s Park" "city":"Penshurst" "state":"NSW" "country":"AUSTRALIA" "id":"INSWPENS2" "lat":-33.958290 "lon":-208.924866 "distance_km":9 "distance_mi":5 } { "neighborhood":"Searl Road" "city":"Cronulla" "state":"NSW" "country":"AUSTRALIA" "id":"INSWCRON2" "lat":-34.052074 "lon":151.148605 "distance_km":11 "distance_mi":7 } { "neighborhood":"Cronulla - Cecil Apartments" "city":"Cronulla - Sydney" "state":"NSW" "country":"AUSTRALIA" "id":"INSWSYDN19" "lat":-34.054329 "lon":151.153656 "distance_km":11 "distance_mi":7 } { "neighborhood":"The Shire - Caringbah South" "city":"Sydney" "state":"New South Wales" "country":"Australia" "id":"INSWCARI2" "lat":-34.048203 "lon":151.120697 "distance_km":12 "distance_mi":7 } { "neighborhood":"Kareela" "city":"Kareela" "state":"NSW" "country":"AUSTRALIA" "id":"INSWKIRR2" "lat":-34.020538 "lon":151.075287 "distance_km":12 "distance_mi":7 } { "neighborhood":"Caringbah South" "city":"Caringbah South" "state":"NSW" "country":"AUSTRALIA" "id":"INSWCARI3" "lat":-34.054550 "lon":151.115799 "distance_km":13 "distance_mi":7 } { "neighborhood":"Sutherland Shire" "city":"Caringbah South" "state":"NEW SOUTH WALES" "country":"Australia" "id":"INEWSOUT125" "lat":-34.059315 "lon":151.117416 "distance_km":13 "distance_mi":8 } { "neighborhood":"Lower North Shore" "city":"Linley Point (Lane Cove)" "state":"NEW SOUTH WALES" "country":"AUSTRALIA" "id":"INEWSOUT83" "lat":-33.826977 "lon":151.151077 "distance_km":13 "distance_mi":8 } { "neighborhood":"Gymea Bay South of Sydney near Royal National Park" "city":"Sydney" "state":"NSW" "country":"Australia" "id":"ISYDNEY2" "lat":-34.047691 "lon":151.079971 "distance_km":14 "distance_mi":8 } { "neighborhood":"Willoughby" [some more text here] --------------------------- mph: 7, 

问题

这是Weather Underground对两个不同城市的回复。 在孟买的情况下,它只提供有关一个城市的数据,但在悉尼的情况下,它提供了悉尼周围许多地方的数据。

我希望能够以一致的方式从此JSON回复中提取信息。 到目前为止,我使用String方法进行一些不那么可靠的提取。
我该怎么做呢?

 package test; import java.net.*; import java.io.*; import org.apache.commons.lang3.text.WordUtils; import com.google.gson.*; import com.google.gson.stream.JsonReader; public class TestGson { URL callToApi; //------------------------------------------------------------------------------ public static void main(String[] args) { new TestGson(); } //------------------------------------------------------------------------------ public TestGson(){ try{ sendCallToApi(); readJson(); }catch(IOException e){ e.printStackTrace(); } } //------------------------------------------------------------------------------ public void sendCallToApi(){ try{ callToApi = new URL("http://api.wunderground.com/api" + "/[API KEY HERE]" + "/geolookup/conditions/forecast/q/" + "Australia/" + "Sydney.json"); }catch(MalformedURLException e){ e.printStackTrace(); } } //------------------------------------------------------------------------------ public void readJson() throws IOException{ Gson g = new Gson(); String wdf = null; InputStreamReader in = new InputStreamReader(callToApi.openStream()); BufferedReader reader = new BufferedReader(in); String message = "hello"; StringBuffer buf = new StringBuffer(); while(true){ message = reader.readLine(); if(message == null){ break; }else{ if(message.contains("mph")){ wdf = message; } buf.append(message); } } message = buf.toString(); System.out.println(message.replaceAll(",","\n")); System.out.println("---------------------------"); wdf = wdf.replace("\"", ""); System.out.println(wdf); } //------------------------------------------------------------------------------ } 

JSON是一个序列化对象 – 这意味着它非常适合反序列化。 这允许您将JSON转换为可以随意查询的java对象。
首先你需要一个合适的java bean – 你的JSON结构很简单,所以不应该很难。
然后,您可以使用Jackson等工具读取数据并将其放入bean中。