使用Gson

时间:2016-03-28 06:25:25

标签: java json gson wunderground

我正在尝试使用GSON java库从下面提供的URL中提取JSON中的值: http://api.wunderground.com/api/b28d047ca410515a/forecast/q/-33.912,151.013.json

我已成功使用下面提供的代码从以下网址中提取数据: http://api.wunderground.com/api/b28d047ca410515a/conditions/q/-33.912,151.013.json

代码:

   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;
    }

2 个答案:

答案 0 :(得分:1)

您遇到的问题是因为getAtPath实施存在问题。

[{"date":{"epoch":"1459152000"...表示该方法尝试作为JSONObject访问的JSONArray。因此IllegalStateException

  

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;
    }

答案 1 :(得分:0)

这应该有效:

System.out.println("max Temperature:" + getAtPath(je2, "forecast/simpleforecast/forecastday/high/celsius").getAsString() );