Wie extrahiere ich sinnvoll Informationen aus einer JSON-Antwort? [Duplikat]
Diese Frage hat hier bereits eine Antwort:
Lesen einer JSON-Antwort von WeatherUnderground 1 AntwortAntwort für Mumbai, Indien{ "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://icons-ak.wxug.com/graphics/wu2/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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://icons-ak.wxug.com/i/c/k/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,
Antwort für Sydney, Australien{ "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,
ProblemDies ist die Antwort vonWeather Underground
für zwei verschiedene Städte. Im Fall von Mumbai werden nur Daten zu einer Stadt angegeben. Im Fall von Sydney werden jedoch Daten zu vielen Orten in der Umgebung von Sydney angezeigt.
Ich möchte in der Lage sein, Informationen daraus zu extrahierenJSON
antworten Sie konsequent. Soweit benutze ichString
Methoden für einige nicht so zuverlässige Extraktion.
Wie mache ich das?
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);
}
//------------------------------------------------------------------------------
}