Java 使用Gson从Json中提取值
我正在尝试使用GSON java库从下面提供的URL中提取JSON值: 我已成功使用下面提供的代码从下面的URL提取数据: 代码:Java 使用Gson从Json中提取值,java,json,gson,wunderground,Java,Json,Gson,Wunderground,我正在尝试使用GSON java库从下面提供的URL中提取JSON值: 我已成功使用下面提供的代码从下面的URL提取数据: 代码: String url = "http://api.wunderground.com/api/b28d047ca410515a/conditions/q/-33.912,151.013.json"; String url2 = "http://api.wunderground.com/api/b28d047ca410515a/forecast/q/-3
String url = "http://api.wunderground.com/api/b28d047ca410515a/conditions/q/-33.912,151.013.json";
String url2 = "http://api.wunderground.com/api/b28d047ca410515a/forecast/q/-33.912,151.013.json";
InputStream is = null;
try {
is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JsonElement je = new JsonParser().parse(jsonText);
System.out.println("Current Temperature:" + getAtPath(je, "current_observation/temp_c").getAsString() );
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
然而,根据下面的代码,我试图从url2中提取时遇到异常,从中获取值似乎是一个更复杂的json,请提供帮助
// below code not working
weather_icon_url = getAtPath(je, "current_observation/icon_url").getAsString();
is = new URL(url2).openStream();
BufferedReader rd2 = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText2 = readAll(rd2);
JsonElement je2 = new JsonParser().parse(jsonText2);
System.out.println("max Temperature:" + getAtPath(je2, "forecast/simpleforecast/forecastday/high/celsius").getAsString() );
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
getAtPath代码:
private static JsonElement getAtPath(JsonElement e, String path) {
JsonElement current = e;
String ss[] = path.split("/");
for (int i = 0; i < ss.length; i++) {
current = current.getAsJsonObject().get(ss[i]);
}
return current;
}
private静态JsonElement getAtPath(JsonElement e,字符串路径){
JsonElement电流=e;
字符串ss[]=path.split(“/”);
对于(int i=0;i
这应该可以:
System.out.println("max Temperature:" + getAtPath(je2, "forecast/simpleforecast/forecastday/high/celsius").getAsString() );
您面临的问题是因为
getAtPath
实现存在问题
[{“date”:{“epoch”:“1459152000”…
表示该方法试图作为JSONObject访问的JSONArray。因此出现了非法状态异常
JsonObject com.google.gson.JsonElement.getAsJsonObject()
将此元素作为JsonObject获取的方便方法
是其他类型的,则会产生IllegalStateException。因此
最好在确保此元素为
通过首先调用isJsonObject()来创建所需的类型
您可以更新并使用如下内容,现在它只返回第一个元素
private static JsonElement getAtPath(JsonElement e, String path) {
JsonElement current = e;
String ss[] = path.split("/");
for (int i = 0; i < ss.length; i++) {
if(current instanceof JsonObject){
current = current.getAsJsonObject().get(ss[i]);
} else if(current instanceof JsonArray){
JsonElement jsonElement = current.getAsJsonArray().get(0);
current = jsonElement.getAsJsonObject().get(ss[i]);
}
}
return current;
}
private静态JsonElement getAtPath(JsonElement e,字符串路径){
JsonElement电流=e;
字符串ss[]=path.split(“/”);
对于(int i=0;i
不工作,它正在引发以下异常:由以下原因引起:java.lang.IllegalStateException:不是JSON对象:[{“日期”:{“历元”:“1459152000”,“……这是因为返回的JSON实际上是一个数组。您也可以发布getAtPath方法吗?您得到了什么异常?请提供getAtPath
的实现。我得到了以下异常:原因:java.lang.IllegalStateException:不是JSON对象:[{“date”:{“epoch”:“1459152000”,“……不客气。请不要忘记接受答案:P