1

I am trying to retrieve specific fields from a JSON file that is being retrieved from Wunderground.com.

I tried to post the relevant information in this, but couldn't get it formatted correctly. I am trying to retrieve the longitude and latitude, under the "current_observation" section. I am using Gson 2.2.4. This is the code I currently have:

    String key = "aaaaaaaaaaaaaaaa";
    String sURL = "http://api.wunderground.com/api/" + key + "/conditions/forecast/q/19104.json";

    URL url = new URL(sURL);
    URLConnection request = (URLConnection) url.openConnection();
    request.connect();

    JsonParser jp = new JsonParser(); //from gson
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
    JsonObject rootobj = root.getAsJsonObject(); 
    JsonElement latitude = rootobj.get("current_observation");

    System.out.println(latitude);

This currently gets everything under the "current_observation" tag, and prints it to the screen. I can not figure out how to access anything under that. I saw several posts on here about using a JsonArray, but no matter what I tried, I could not get it to work correctly. So how do I retrieve a specific field from a JSON file? Thank you for any guidance you can give me, and please let me know if I should provide any additional information.

2 Answers 2

3

A JsonElement is a common interface which is subclassed by two important other classes which are JsonArray or JsonObject.

Since you are not providing Gson a type to reflect the information from (and fill a corresponding object) you must go by hand. Since "current_observation" is a dictionary type then it's a JsonObject and you can do:

 JsonObject observation = root.getAsJsonObject().get("current_observation").getAsJsonObject();

At this point you can retrieve specific fields like you were doing previously:

float longitude = observation.get("longitude").getAsFloat();

and so on.

For specific field you may want to provide custom deserializer or serializer. Actually the best solution would be to have your mirrored structure in code repository, eg:

class Observation
{
  float latitude;
  float longitude;
  // other fields you are interested in
}

so that you can provide your own deserializer and do:

Observation observation = gson.fromJson(root.getAsJsonObject().get("current_observation"), Observation.class)

and let Gson do the dirty work.

Sign up to request clarification or add additional context in comments.

3 Comments

In this case you actually need to retrieve another JsonObject, because latitude is in current_observation > display_location > latitude. Or current_observation > observation_location > latitude, because there are actually two latitude in that JSON file, I don't know which one he wants.
@ArianJM: Yes the structure is irrelevant in any case, I would like to make him understand the basic principle behind this. Which is traversing the tree through getAsJsonObject and getAsJsonArray. But having a custom type adapter is still the preferable solution IMHO
Thank you so much! Using your example, I was able to get the latitude and longtitude like I needed, after creating another JsonObject like @ArianJM said, for the display_location. This was an excellent description, and really helped me understand how Json is supposed to work.
2

Now as your current_observation JSON itself contains some JSON as well as String fileds. I will tell you for 1 string feild station_id and other JSON field image.You can use like this :-

            JsonParser jp = new JsonParser(); //from gson
            JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
            JsonObject rootobj = root.getAsJsonObject(); 
            JSONObject curObs = (JSONObject)rootobj.get("current_observation");
            JSONObject image = (JSONObject)curObs.get("image"); // image is a JSON
            String imageUrl= (String)image.get("url"); // get image url
            String stationId = (String)curObs.get("station_id"); // get StationId

Similarly you can do for other attributes of JSON also. Hope this helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.