1

I am trying to extract values from JSON from the URL provided below using GSON java library: http://api.wunderground.com/api/b28d047ca410515a/forecast/q/-33.912,151.013.json

I have successfully used the code provided below to extract data from URL below: http://api.wunderground.com/api/b28d047ca410515a/conditions/q/-33.912,151.013.json

Code:

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

However I am getting exception trying to extract from url2 as per code below , it seems to be a more complicated json to get values from, any help please?

// 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 code:

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;
    }
Rahul
  • 15,979
  • 4
  • 42
  • 63
Ossama
  • 2,401
  • 7
  • 46
  • 83
  • What exception are you getting? Please provide the implementation of `getAtPath`. – dShringi Mar 28 '16 at 10:29
  • I get the following exception: Caused by: java.lang.IllegalStateException: Not a JSON Object: [{"date":{"epoch":"1459152000","........... – Ossama Mar 28 '16 at 10:31

2 Answers2

1

The problem you are facing is because there is an issue with the getAtPath implementation.

[{"date":{"epoch":"1459152000"... represents a JSONArray which the method is trying to access as JSONObject. Hence the IllegalStateException.

JsonObject com.google.gson.JsonElement.getAsJsonObject()

convenience method to get this element as a JsonObject. If the element is of some other type, a IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling isJsonObject() first.

You can update and use something like below, as of now it returns only the first element.

    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;
    }
dShringi
  • 1,497
  • 2
  • 22
  • 36
0

This should work:

System.out.println("max Temperature:" + getAtPath(je2, "forecast/simpleforecast/forecastday/high/celsius").getAsString() );
Milan
  • 1,780
  • 1
  • 16
  • 29
  • not working, it is throwing the following exception: Caused by: java.lang.IllegalStateException: Not a JSON Object: [{"date":{"epoch":"1459152000","........... – Ossama Mar 28 '16 at 08:53
  • 1
    That is because the JSON returned is actually an array. Can you post getAtPath method as well? – Milan Mar 28 '16 at 10:31