简体   繁体   中英

Retrieving specific fields from JSON file in Java

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM