- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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);
}
//------------------------------------------------------------------------------
}
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
是什么导致了这些错误?如何正确调用 Weather Underground API 来接收天气信息?
我通过修改 Mykong.com 上给出的示例来尝试此操作。
{ "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
最佳答案
你似乎不明白Gson
是做什么的。它是一个 JSON 解析器,特别是将 JSON 反序列化为 Java POJO 的解析器。
在您的代码中:
String wdf = g.fromJson(message, String.class);
您告诉 Gson 将 JSON 反序列化为 Java String
对象。它不能那样做;您的 JSON 不是 String
对象 - 它是从 Weather Underground 返回的一个大 JSON 对象。您已经有一个包含 JSON 文本本身的 Java String
(message
)。
您需要创建一个 JSON 可以反序列化的 POJO,或者使用 Gson 的解析器和关联的对象/方法从 JSON 中选择您想要的部分。
下面是 POJO 的样子(所有字段都是公共(public)的,没有 getter/setter,只是为了使这个示例更小):
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;
}
然后,您可以使用以下命令将 JSON 反序列化为 MyWUPojo
实例:
MyWUPojo wup = new Gson().fromJson(message,MyWUPojo.class);
如果您不想走 POJO 路线,您可以解析 JSON,然后从中获取您想要的内容:
JsonObject o = (JsonObject) new JsonParser().parse(message);
现在您可以使用 JsonObject
的各种方法来访问解析后的 JSON。 (参见:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonObject.html)
GSON User's Guide写得很好,并详细解释了这一点。
编辑添加: JSON 末尾的 null
是由以下原因引起的:
while(message!=null){
message = reader.readLine();
buf.append(message);
}
您附加null
然后循环退出。它需要是:
while ((message = reader.readline()) != null) {
buf.append(message);
}
关于java - 读取来自 Weather Underground 的 JSON 回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826237/
我有多个 InOut 模式的顺序队列,每个队列通向一个 Camel 处理器。如果处理器花费太多时间进行处理,我希望请求-答复检测到超时并发送 ExchangeTimedOutException。Cam
我是一名学生,试图了解更多关于 C# 中的 ARP 和套接字的信息 为此,我尝试使用原始 Socket 发送 ARP 请求和回复。在 C# 中。 我已经在一个字节数组中手动重建了一个 ARP 回复
我正在使用 Amazon Web Services 数据库 dynamodb。它返回一个 JSON,看起来像这样: {"Responses":{"friends":[{"to_username":"u
我有 servlet,它创建 html 文件,然后将其转换为 pdf 文件: private void ConvertHTMLtoPDF(String sConvertationProgramm, S
我正在为我的项目使用 Mailkit 库 (Imap)。 我可以轻松地通过 SmtpClient 发送新消息。 目前我正在研究如何回复特定邮件。是否可以向该回复邮件添加更多收件人? @jstedfas
这个问题已经有答案了: How to parse JSON in Java (36 个回答) 已关闭 7 年前。 请您告诉我如何解析 JSON 回复。我从 https://api.privatbank
我正在尝试从事 YouTube 视频评级工作。但我被困在这里了。这两个 YouTube 评级请求有什么区别? https://www.googleapis.com/youtube/v3/videos/
我搜索了该网站,令人惊讶的是没有找到任何适合我情况的答案。所以我发布了这个问题。 我正在使用 jQuery AJAX 来获取网页并将其动态添加到网站。但我的代码不起作用并抛出错误 ERROR Type
我正在编写一个 C 程序来搜索本地网络上的 smb 共享并安装它们。为了获取找到的 smb 服务器的主机名,我向服务器发送了 udp nbns 数据包。数据包正确,服务器回复。我正在使用wiresha
我正在尝试使用 Java 创建自己的 WebSocket 服务器。 当我的客户端连接时,我收到以下请求: (14): GET / HTTP/1.1 (18): Upgrade: WebSocket (
我正在使用 Nodejs、Express、MySQL、EJS。 用户能够创建帖子和评论/回复评论/回复对这些帖子的回复。 问题:我不知道如何以允许我在 EJS 中呈现它们的方式将数据分类为对象/数组。
我正在用 .NET c++ 编写程序。我正在本地网络上发送广播 ping。我的所有目标设备都能够响应 ping 广播,事实上我在 Wireshark 中跟踪了它们的所有响应。我的目标是检索所有响应站点
我有一个向 JMS MDB 发送请求的客户端。它可以很好地向 MDB 发送消息,但我一辈子都无法弄清楚如何让客户端接收 MDB 发回给它的响应。 编辑:客户端代码是同一实例上的 Web 服务,使用 @
我使用钩子(Hook)函数制作了一个模块。它可以工作,但是当我使用 ping google.com 时,我得到 0045 作为 icmp 类型。但我认为它应该是 0 用于回显回复。我使用了以下打印命令
我正在用 C 从头开始编写一个网络库。我已经实现了以太网协议(protocol),现在我想让 ARP 工作。发送请求/回复工作正常,但接收工作不正常。当我发送一个发送请求并等待它之后的回复时,re
我在替换字符串的一部分时遇到问题。现在这段代码。我的目标是针对包含此字典中的键的每个字符串。 mapping = { "St": "Street", "St.": "Stree
我有一个 WCF p2p 网状网络,它运行良好,适合单向对话。我正在研究是否可以调用一种方法来添加两个数字并返回和总和。 但是我在尝试连接时遇到错误: 契约(Contract)需要请求/回复,但绑定(
大家好,我正在尝试使用分块编码流式传输多媒体数据。因此,我首先尝试使用分块编码发送文本数据。 这是我的代码。我创建了一个服务器套接字,我在上面监听请求(端口 80),回复请求,然后我的程序终止。无论您
我的 Servlet 中有一些图像,我想将它们下载到我的 Android 应用中。 我正在对这个 URL 执行一个 GET 请求: public static final String URL ="h
文档:https://developers.google.com/apps-script/reference/gmail/gmail-message#replybody-options 当跟进一封电子
我是一名优秀的程序员,十分优秀!