从WeatherUnderground读取JSON回复 [英] Reading a JSON reply from WeatherUnderground
问题描述
package test;
导入java.net。*;
import java.io. *;
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(){
尝试{
sendCallToApi();
readJson();
} catch(IOException e){
e.printStackTrace();
}
}
// ---------------------------------- --------------------------------------------
public void sendCallToApi(){
try {
callToApi = new URL(http://api.wunderground.com/api+
/ [insert key here]+
/ geolookup / conditions / forecast / q /+
Australia /+
Sydney.json);
} catch(MalformedURLException e){
e.printStackTrace();
}
}
// ---------------------------------- --------------------------------------------
public void readJson()抛出IOException {
Gson g = new Gson();
InputStreamReader in = new InputStreamReader(callToApi.openStream());
BufferedReader reader = new BufferedReader(in);
String message =hello;
StringBuffer buf = new StringBuffer();
while(message!= null){
message = reader.readLine();
buf.append(message);
}
message = buf.toString();
String wdf = g.fromJson(message,String.class);
System.out.println(wdf);
}
// --------------------------------------- ---------------------------------------
}
这是我第一次尝试使用GSON解析JSON。我只是打电话给Weather Underground网站,要求他们寄给我澳大利亚悉尼的天气。
但是,我得到了多个错误:
线程main中的异常com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串,但是在第1行的BEGIN_OBJECT第2列
在com.google .gson.Gson.fromJson(Gson.java:806)
在com.google.gson.Gson.fromJson(Gson.java:761)
在com.google.gson.Gson.fromJson(Gson .java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at test.TestGson.readJson(TestGson.java:49)
at test。 TestGson。< init>(TestGson.java:19)
at test.TestGson.main(TestGson.java:13)
引起:java.lang.IllegalStateException:期望一个字符串,但是BEGIN_OBJECT在第1行第2列
位于com.google.gson.stream.JsonReader.nextString(JsonReader.java:464)
位于com.google.gson.internal.bind.TypeAdapters $ 13.read(TypeAdapters.java :349)
在com.google.gson.internal.bind.TypeAdapters $ 13.read(TypeAdapters.java:337)
在com.google.gson.Gson.fromJson(Gson.java:795)
。 .. 6更多
什么导致了这些错误?我如何正确地呼叫Weather Underground API以接收天气信息?
我通过修改Mykong.com上给出的示例尝试了此操作。
JSON地下天气的回复
{response:{ 版本:0.1,服务条款:http://www.wunderground.com/weather/api/d/terms.html,功能:{geolookup:1,条件:1 ,位置:{type:INTLCITY,country:SD,country_iso3166:SA,country_name:沙特阿拉伯,州 :,city:Jeddah,tz_short:AST,tz_long:亚洲/利雅得,lat:21.67000008,lon:39.15000153,zip: 00000,magic:1,wmo:41024,l:/ q / zmw:00000.1.41024,requesturl:global / stations / 41024.html wuiurl:http://www.wunderground.com/global/stations/41024.html,nearby_weather_stations:{airport:{station:[{city:吉达国王阿卜杜勒阿齐兹国际机场,国家:,国家:沙特阿拉伯,icao:O EJN,lat:21.70000076,lon:39.18333435},{city:Jeddah,state:,country:SD,icao:OEJN lat:21.67000008,lon:39.15000153},{city:Makkah,state:,country:SD,icao ,lat:21.43000031,lon:39.77000046}]},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 :沙特阿拉伯,沙特阿拉伯,城市 SA,zip:00000,纬度:21.67000008,经度:39.15000153,海拔:12.00000000},observation_location:{full:Jeddah 城市:吉达,国家:,国家:SD, country_iso3166:SA,纬度:21.67000008,经度:39.15000153,高程:39英尺},估计:{},station_id:OEJN观察时间:最后更新时间为2月12日上午9:00 AST,observation_time_rfc822:星期二,2013年2月12日09:00:00 +0300,observation_epoch:1360648800,local_time_rfc822:星期二,2013年2月12日09时08分53秒+0300,local_epoch:1360649333,local_tz_short:AST,local_tz_long:Asia / Riyadh,local_tz_offset:+ 0300 :Clear,temperature_string:77F(25C),temp_f:77,temp_c:25,ative_humidity:57%,wind_string: MPH,wind_dir,North,wind_degrees10,wind_mph10,wind_gust_mph0,wind_kph17,wind_gust_kph0,pressure_mb,1011 pressure_in:29.86,pressure_trend:0,露点_字符串:61F(16C),露点t_f:61,dewpoint_c:16,heat_index_string:NA,heat_index_f:NA,heat_index_c:NA,windchill_string:NA,windchill_f ,windchill_c:NA,feellike_string:77F(25C),feellike_f:77,feellike_c:25,visibility_mi:N / A,visibility_km :N / A,solarradiation:,UV: - 1,precipitation_1hr_string: - 9999.00 in(-9999.00mm),precip_1hr_in: - 9999.00,precip_1hr_metric : - 9999.00,precipitation_today_string:0.00 in(0.0 mm),precip_today_in:0.00,precipitation_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/41024.html,history_url: http://www.wunderground.com/history/airport/OEJN/2013/2/12/DailyHistory.html,ob_url:http://www.wunderground.com/cgi-bin/findweather/getForecast ?查询= 21.67000008,39.15000153\" },forecast:{txt_forecast:{date:3:00 AM AST,forecastday:[{period:0,icon:partiallycloudy,icon_url:http ://icons-ak.wxug.com/i/c/k/partlycloudy.gif,标题:星期二,fcttext:清晨,晴间多云。 84F高。凉风习习。 北风从10至20英里/小时。,fcttext_metric:早上晴,一会儿晴间多云。高29C。凉风习习。风速从15至30公里/小时。,pop:0},{period:1,icon:clear,icon_url:http:// icons-ak .wxug.com / i / c / k / clear.gif,标题:星期二晚上,fcttext:清除。 70F的低。以10-15英里/小时的速度从北大西洋风吹过。,fcttext_metric:清除。低于21C。凉风习习。风速从15至25公里/小时。,pop:0},{period:2,icon:partiallycloudy,icon_url:http:// icons-ak .wxug.com / i / c / k / partiallycloudy.gif,title:星期三,fcttext:早上晴朗,然后局部多云。 88F高。风力从北方风力的5到15英里/小时。,fcttext_metric:晴朗,早上晴间多云。 31C高。凉风习习。风速从10到25 km / h。,pop:0},{period:3,icon:clear,icon_url:http:// icons-ak .wxug.com / i / c / k / clear.gif,标题:周三晚上,fcttext:晚上晴朗,然后局部多云。 70F的低。西北风力5至15英里/小时。,fcttext_metric:晴朗,傍晚时分偏多云。低于21C。凉风习习。风速从10到25公里/小时。,pop:0},{period:4,icon:partlycloudy,icon_url:http:// icons-ak .wxug.com / i / c / k / partiallycloudy.gif,标题:星期四,fcttext:清晨,晴间多云。 88F高。风速从北方以5至10英里/小时的速度在下午转移至WSW。,fcttext_metric:清晨,晴间多云。 31C高。 ,pop:0},{period:5,icon:清除,icon_url:从北方向以5至15公里/小时的速度向下移动至WSW。 http://icons-ak.wxug.com/i/c/k/clear.gif,title:星期四之夜,fcttext:清晰。 73F的低。风力从WNW上5到10英里/小时。,fcttext_metric:清除。低23C。 WNW以5至15公里/小时的风速,pop:0},{period:6,icon:clear,icon_url:http:// icons-ak .wxug.com / i / c / k / clear.gif,title:星期五,fcttext:清除。 88F高。 NNE以5至10英里/小时的速度在下午转移到WSW。,fcttext_metric:晴。 31C高。 NNE以5至20公里/小时的速度在下午转移至WSW。,pop:0},{period:7,icon:clear,icon_url: http://icons-ak.wxug.com/i/c/k/clear.gif,title:星期五之夜,fcttext:清除。低于72F。风速从5到10英里/小时。,fcttext_metric:清除。低于22C。风速从5到15公里/小时。,pop:0}]},simpleforecast:{forecastday:[{date:{epoch:1360692000,漂亮:2013年2月12日9:00 AST,日:12,月:2,年:2013年,yday:42,小时:21,min: 00,sec:0,isdst:0,monthname:2月,weekday_short:Tue,weekday:Tuesday,ampm:PM, tz_short:AST,tz_long:Asia / Riyadh,period:1,high:{fahrenheit:84,celsius:29}, :{fahrenheit:70,celsius:21},conditions:部分多云,icon:partiallycloudy,icon_url:http://icons-ak.wxug .com / i / c / k / partiallycloudy.gif,skyicon:mostlysunny,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:17,kph:27,dir:NW,度:317},avewind:{mph:14,kph:22,dir ,度数:324},平衡度:52,最大湿度:74,最小湿度:43},{日期:{epoch:1360778400,漂亮 2013年2月13日的PM AST,日:13,月:2,年:2013,yday:43,小时:21,min:00 :0,月,月,月,星期 ,tz_long:Asia / Riyadh},period:2,high:{fahrenheit:88,celsius:31},low:{fahrenheit 70,摄氏,21,条件:部分多云,图标:partiallycloudy,icon_url:http:// icons- ak.wxug.com/i/c/k/partlycloudy.gif,skyicon:partiallycloudy,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 kph:22,dir:NW,度:308},avewind:{mph:12,kph:19,dir:NW,度 315,avehumidity:51,maxhumidity:75,minhumidity:39},{date:{epoch:1360864800,pretty 2013,yday:44,hour:21,min:00,sec:0,isdst :0,monthname:2月,weekday_short:星期四,星期几:星期四,ampm:P M,tz_short:AST,tz_long:Asia / Riyadh},period:3,high:{fahrenheit:88,celsius:31 low:{fahrenheit:73,celsius:23},conditions:Partly Cloudy,icon:partiallycloudy,icon_url:http:// icons- ak.wxug.com/i/c/k/partlycloudy.gif,skyicon:mostlysunny,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 kph:14,dir:West,度:259},avewind:{mph:7,kph:11,dir:WSW,度 250},avehumidity:51,maxhumidity:69,minhumid ity:37,{date:{epoch:1360951200,pretty:9:00 PM 2013年2月15日AST,day:15,month:2, yday:45,hour:21,min:00,sec:0,isdst:0,monthname:February,weekday_short :星期五,星期几,星期五,安眠,PM,tz_short:AST,tz_long:亚洲/利雅得},时间段:4, {fahrenheit:88,celsius:31},low:{fahrenheit:72,celsius:22},conditions:Clear, :clear,icon_url:http://icons-ak.wxug.com/i/c/k/clear.gif,skyicon:mostlysunny,pop:0,qpf_allday :{in:0.00,mm:0.0},qpf_day:{in:0.00,mm:0.0},qpf_night:{in:0.00,mm },snow_allday:{in:0,cm:0},snow_day:{in:0,cm:0},snow_night:{in:0,厘米: 0},maxwind:{mph:10,kph:16,dir:WSW,度:248},avewind:{mph:7,kph 11,dir:WSW,度:248},avehumidity:53,maxhumidity:60,minhumidity:38}]}}} null
您似乎不明白 Gson
确实。这是一个JSON解析器,特别是将JSON反序列化为Java POJO的解析器。
在你的代码中有:
字符串wdf = g。 fromJson(message,String.class);
您告诉Gson将JSON反序列化为Java 您需要创建一个POJO,以便JSON可以反序列化,或者使用Gson的解析器和关联的对象/方法从您想要的JSON中选择部分。 以下是POJO看起来的样子(所有公开的没有getter / setter的字段只是为了让这个例子变小): 然后,您可以将JSON反序列化为 如果你不想去POJO路线,你可以解析JSON然后得到你想要的从它: 现在您可以使用 GSON用户指南编写得很好,并对此进行了详细解释。 / p> 编辑以添加:您的JSON结尾处的 您追加 String
对象。它不能那样做;你的JSON 不是一个
String
对象 - 它是从Weather Underground返回的一个很大的JSON对象。您已经拥有包含JSON文本本身的Java String
( message
)。
class MyWUPojo {
public response response;
//在此之后匹配JSON
}
class响应{
public String version;
public String termsofservice;
public Map< String,Integer>特征;
}
MyWUPojo
with:
MyWUPojo wup = new Gson()。fromJson(message,MyWUPojo.class );
JsonObject o =(JsonObject)new JsonParser()。parse(message);
JsonObject
来访问解析的JSON。 (请参阅: http:// google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonObject.html )
null
是由此导致的:
while(message!= null){
message = reader.readLine();
buf.append(message);
}
null
然后
$ b
while((message = reader.readline())!= null){
buf .append(消息);
}
package test;
import java.net.*;
import java.io.*;
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" +
"/[insert key here]" +
"/geolookup/conditions/forecast/q/" +
"Australia/" +
"Sydney.json");
}catch(MalformedURLException e){
e.printStackTrace();
}
}
//------------------------------------------------------------------------------
public void readJson() throws IOException{
Gson g = new Gson();
InputStreamReader in = new InputStreamReader(callToApi.openStream());
BufferedReader reader = new BufferedReader(in);
String message = "hello";
StringBuffer buf = new StringBuffer();
while(message!=null){
message = reader.readLine();
buf.append(message);
}
message = buf.toString();
String wdf = g.fromJson(message, String.class);
System.out.println(wdf);
}
//------------------------------------------------------------------------------
}
This is my first attempt at parsing JSON using GSON. I am simply making a call to the Weather Underground site asking them to send me the weather of Sydney, Australia.
However, I get multiple errors:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2
at com.google.gson.Gson.fromJson(Gson.java:806)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at test.TestGson.readJson(TestGson.java:49)
at test.TestGson.<init>(TestGson.java:19)
at test.TestGson.main(TestGson.java:13)
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2
at com.google.gson.stream.JsonReader.nextString(JsonReader.java:464)
at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:349)
at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:337)
at com.google.gson.Gson.fromJson(Gson.java:795)
... 6 more
What is causing those errors? How do I properly make the call to the Weather Underground API to receive the weather info?
I tried this by modifying the example given on Mykong.com.
JSON Reply from Weather Underground
{ "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":"SD", "country_iso3166":"SA", "country_name":"Saudi Arabia", "state":"", "city":"Jeddah", "tz_short":"AST", "tz_long":"Asia/Riyadh", "lat":"21.67000008", "lon":"39.15000153", "zip":"00000", "magic":"1", "wmo":"41024", "l":"/q/zmw:00000.1.41024", "requesturl":"global/stations/41024.html", "wuiurl":"http://www.wunderground.com/global/stations/41024.html", "nearby_weather_stations": { "airport": { "station": [ { "city":"Jeddah King Abdul Aziz International Airport", "state":"", "country":"Saudi Arabia", "icao":"OEJN", "lat":"21.70000076", "lon":"39.18333435" }, { "city":"Jeddah", "state":"", "country":"SD", "icao":"OEJN", "lat":"21.67000008", "lon":"39.15000153" }, { "city":"Makkah", "state":"", "country":"SD", "icao":"OEMK", "lat":"21.43000031", "lon":"39.77000046" } ] } , "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":"Jeddah, Saudi Arabia", "city":"Jeddah", "state":"", "state_name":"Saudi Arabia", "country":"SD", "country_iso3166":"SA", "zip":"00000", "latitude":"21.67000008", "longitude":"39.15000153", "elevation":"12.00000000" }, "observation_location": { "full":"Jeddah, ", "city":"Jeddah", "state":"", "country":"SD", "country_iso3166":"SA", "latitude":"21.67000008", "longitude":"39.15000153", "elevation":"39 ft" }, "estimated": { }, "station_id":"OEJN", "observation_time":"Last Updated on February 12, 9:00 AM AST", "observation_time_rfc822":"Tue, 12 Feb 2013 09:00:00 +0300", "observation_epoch":"1360648800", "local_time_rfc822":"Tue, 12 Feb 2013 09:08:53 +0300", "local_epoch":"1360649333", "local_tz_short":"AST", "local_tz_long":"Asia/Riyadh", "local_tz_offset":"+0300", "weather":"Clear", "temperature_string":"77 F (25 C)", "temp_f":77, "temp_c":25, "relative_humidity":"57%", "wind_string":"From the North at 10 MPH", "wind_dir":"North", "wind_degrees":10, "wind_mph":10, "wind_gust_mph":0, "wind_kph":17, "wind_gust_kph":0, "pressure_mb":"1011", "pressure_in":"29.86", "pressure_trend":"0", "dewpoint_string":"61 F (16 C)", "dewpoint_f":61, "dewpoint_c":16, "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":"N/A", "visibility_km":"N/A", "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/41024.html", "history_url":"http://www.wunderground.com/history/airport/OEJN/2013/2/12/DailyHistory.html", "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=21.67000008,39.15000153" } , "forecast":{ "txt_forecast": { "date":"3:00 AM AST", "forecastday": [ { "period":0, "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "title":"Tuesday", "fcttext":"Clear in the morning, then partly cloudy. High of 84F. Breezy. Winds from the NNW at 10 to 20 mph.", "fcttext_metric":"Clear in the morning, then partly cloudy. High of 29C. Breezy. Winds from the NNW at 15 to 30 km/h.", "pop":"0" } , { "period":1, "icon":"clear", "icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif", "title":"Tuesday Night", "fcttext":"Clear. Low of 70F. Winds from the NNW at 10 to 15 mph.", "fcttext_metric":"Clear. Low of 21C. Breezy. Winds from the NNW at 15 to 25 km/h.", "pop":"0" } , { "period":2, "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "title":"Wednesday", "fcttext":"Clear in the morning, then partly cloudy. High of 88F. Winds from the NNW at 5 to 15 mph.", "fcttext_metric":"Clear in the morning, then partly cloudy. High of 31C. Breezy. Winds from the NNW at 10 to 25 km/h.", "pop":"0" } , { "period":3, "icon":"clear", "icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif", "title":"Wednesday Night", "fcttext":"Clear in the evening, then partly cloudy. Low of 70F. Winds from the NW at 5 to 15 mph.", "fcttext_metric":"Clear in the evening, then partly cloudy. Low of 21C. Breezy. Winds from the NW at 10 to 25 km/h.", "pop":"0" } , { "period":4, "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "title":"Thursday", "fcttext":"Clear in the morning, then partly cloudy. High of 88F. Winds from the North at 5 to 10 mph shifting to the WSW in the afternoon.", "fcttext_metric":"Clear in the morning, then partly cloudy. High of 31C. Winds from the North at 5 to 15 km/h shifting to the WSW in the afternoon.", "pop":"0" } , { "period":5, "icon":"clear", "icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif", "title":"Thursday Night", "fcttext":"Clear. Low of 73F. Winds from the WNW at 5 to 10 mph.", "fcttext_metric":"Clear. Low of 23C. Winds from the WNW 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":"Friday", "fcttext":"Clear. High of 88F. Winds from the NNE at 5 to 10 mph shifting to the WSW in the afternoon.", "fcttext_metric":"Clear. High of 31C. Winds from the NNE at 5 to 20 km/h shifting to the WSW in the afternoon.", "pop":"0" } , { "period":7, "icon":"clear", "icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif", "title":"Friday Night", "fcttext":"Clear. Low of 72F. Winds from the NW at 5 to 10 mph.", "fcttext_metric":"Clear. Low of 22C. Winds from the NW at 5 to 15 km/h.", "pop":"0" } ] }, "simpleforecast": { "forecastday": [ {"date":{ "epoch":"1360692000", "pretty":"9:00 PM AST on February 12, 2013", "day":12, "month":2, "year":2013, "yday":42, "hour":21, "min":"00", "sec":0, "isdst":"0", "monthname":"February", "weekday_short":"Tue", "weekday":"Tuesday", "ampm":"PM", "tz_short":"AST", "tz_long":"Asia/Riyadh"}, "period":1, "high": { "fahrenheit":"84", "celsius":"29" }, "low": { "fahrenheit":"70", "celsius":"21" }, "conditions":"Partly Cloudy", "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "skyicon":"mostlysunny", "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": 17, "kph": 27, "dir": "NW", "degrees": 317 }, "avewind": { "mph": 14, "kph": 22, "dir": "NW", "degrees": 324 }, "avehumidity": 52, "maxhumidity": 74, "minhumidity": 43 } , {"date":{ "epoch":"1360778400", "pretty":"9:00 PM AST on February 13, 2013", "day":13, "month":2, "year":2013, "yday":43, "hour":21, "min":"00", "sec":0, "isdst":"0", "monthname":"February", "weekday_short":"Wed", "weekday":"Wednesday", "ampm":"PM", "tz_short":"AST", "tz_long":"Asia/Riyadh"}, "period":2, "high": { "fahrenheit":"88", "celsius":"31" }, "low": { "fahrenheit":"70", "celsius":"21" }, "conditions":"Partly Cloudy", "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "skyicon":"partlycloudy", "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": 14, "kph": 22, "dir": "NW", "degrees": 308 }, "avewind": { "mph": 12, "kph": 19, "dir": "NW", "degrees": 315 }, "avehumidity": 51, "maxhumidity": 75, "minhumidity": 39 } , {"date":{ "epoch":"1360864800", "pretty":"9:00 PM AST on February 14, 2013", "day":14, "month":2, "year":2013, "yday":44, "hour":21, "min":"00", "sec":0, "isdst":"0", "monthname":"February", "weekday_short":"Thu", "weekday":"Thursday", "ampm":"PM", "tz_short":"AST", "tz_long":"Asia/Riyadh"}, "period":3, "high": { "fahrenheit":"88", "celsius":"31" }, "low": { "fahrenheit":"73", "celsius":"23" }, "conditions":"Partly Cloudy", "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "skyicon":"mostlysunny", "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": 9, "kph": 14, "dir": "West", "degrees": 259 }, "avewind": { "mph": 7, "kph": 11, "dir": "WSW", "degrees": 250 }, "avehumidity": 51, "maxhumidity": 69, "minhumidity": 37 } , {"date":{ "epoch":"1360951200", "pretty":"9:00 PM AST on February 15, 2013", "day":15, "month":2, "year":2013, "yday":45, "hour":21, "min":"00", "sec":0, "isdst":"0", "monthname":"February", "weekday_short":"Fri", "weekday":"Friday", "ampm":"PM", "tz_short":"AST", "tz_long":"Asia/Riyadh"}, "period":4, "high": { "fahrenheit":"88", "celsius":"31" }, "low": { "fahrenheit":"72", "celsius":"22" }, "conditions":"Clear", "icon":"clear", "icon_url":"http://icons-ak.wxug.com/i/c/k/clear.gif", "skyicon":"mostlysunny", "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": "WSW", "degrees": 248 }, "avewind": { "mph": 7, "kph": 11, "dir": "WSW", "degrees": 248 }, "avehumidity": 53, "maxhumidity": 60, "minhumidity": 38 } ] } }}null
You don't seem to understand what Gson
does. It's a JSON parser, specifically one that will deserialize JSON to a Java POJO.
In your code you have:
String wdf = g.fromJson(message, String.class);
You're telling Gson to deserialize the JSON to a Java String
object. It can't do that; your JSON isn't a String
object - it's a big JSON object returned from Weather Underground. You already have a Java String
(message
) that contains the JSON text itself.
You either need to create a POJO that the JSON can be deserialized to, or pick the parts out of the JSON you want using Gson's parser and associated objects/methods.
Here's the start of what a POJO would look like (all fields public with no getters/setters just to make this example small):
class MyWUPojo {
public Response response;
// more after this that match the JSON
}
class Response {
public String version;
public String termsofservice;
public Map<String, Integer> features;
}
You'd then deserialize the JSON into an instance of MyWUPojo
with:
MyWUPojo wup = new Gson().fromJson(message,MyWUPojo.class);
If you didn't want to go the POJO route you could parse the JSON then get what you want from it:
JsonObject o = (JsonObject) new JsonParser().parse(message);
Now you' can use the various methods of JsonObject
to access the parsed JSON. (See: http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonObject.html )
The GSON User's Guide is pretty well written and explains this and more in great detail.
Edit to add: That null
at the end of your JSON is caused by this:
while(message!=null){
message = reader.readLine();
buf.append(message);
}
You append null
then the loop exits. It needs to be:
while ((message = reader.readline()) != null) {
buf.append(message);
}
这篇关于从WeatherUnderground读取JSON回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!