简体   繁体   English

从Java中的JSON文件中检索特定字段

[英]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. 我正在尝试从Wunderground.com检索的JSON文件中检索特定字段。

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. 我正在尝试在“ current_observation”部分下检索经度和纬度。 I am using Gson 2.2.4. 我正在使用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. 当前,这使所有内容都在“ current_observation”标签下,并将其打印到屏幕上。 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. 我在这里看到了有关使用JsonArray的几篇文章,但是无论我尝试了什么,我都无法使其正常工作。 So how do I retrieve a specific field from a JSON file? 那么,如何从JSON文件中检索特定字段? 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 . JsonElement是一个公共接口,它由其他两个重要的子类JsonArrayJsonObject JsonArray

Since you are not providing Gson a type to reflect the information from (and fill a corresponding object) you must go by hand. 由于没有为Gson提供类型来反映来自(并填充相应对象)信息的类型,因此您必须手动操作。 Since "current_observation" is a dictionary type then it's a JsonObject and you can do: 由于"current_observation"是字典类型,因此它是一个JsonObject ,您可以执行以下操作:

 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. 现在,由于您的current_observation JSON本身包含一些JSONString I will tell you for 1 string feild station_id and other JSON field image .You can use like this :- 我会告诉你,1串费尔德station_id和其他JSONimage 。你可以使用这样的: -

            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. 同样,您也可以为JSON其他属性执行操作。 Hope this helped. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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