简体   繁体   English

如何使用GSON从此JSON制作Java对象?

[英]How do I make Java Object from this JSON with GSON?

JSON: JSON:

{"station":[{"id":"p2","websiteId":"p2","name":,"label":"Price","latitude":,"longitude":,"zip":"","region":"South"}],"parameters":{"ozone":{"value":"0.044","unit":" ppm","color":"17,250,11","colorHex":"11fa11","arrow":"unchange_gray"},"pm25":{"value":"NA","unit":"","color":"000000","colorHex":"000000","arrow":""},"temperature":40.4,"wind_speed":4.5,"wind_dir":"SE"},"seasonalParameter":"ozone","forecast":[],"version":"1.5"}

My attempt at the objects: 我对这些物体的尝试:

public class AirObjects {


    @SerializedName("station")
    public List<Station> stationData;



    public class Station {

        @SerializedName("id")
        public String id;

        @SerializedName("parameters")
        public Parameters parameters;
    }

    protected class Ozone
    {
        @SerializedName("value")
        public String ozone_value;

        @SerializedName("unit")
        public String ozone_unit;

        @SerializedName("colorHex")
        public String ozone_color;

        @SerializedName("arrow")
        public String arrow;


    }

    protected class Parameters
    {
        @SerializedName("ozone")
        public Ozone ozone;

        @SerializedName("temperature")
        public String temperature;

        @SerializedName("wind_speed")
        public String windSpeed;

        @SerializedName("wind_dir")
        public String windDirection;
    }

    protected class Sites
    {
        @SerializedName("id")
        public String id;

        @SerializedName("websiteId")
        public String websiteId;

        @SerializedName("name")
        public String name;

        @SerializedName("label")
        public String label;

        @SerializedName("latitude")
        public String latitude;

        @SerializedName("longitude")
        public String longitude;

        @SerializedName("zip")
        public String zip;

        @SerializedName("region")
        public String region;


    }

}

This is the structure of your json (you can use http://jsonviewer.stack.hu to see the structure of a json value): 这是json的结构(您可以使用http://jsonviewer.stack.hu查看json值的结构):

操作问题的json结构

Now you should define the POJO for this structure. 现在,您应该为此结构定义POJO。

  1. You have station array, parameters object, seasonalParameter String, version String and forecast array at first level of your json. 您在json的第一级具有station数组, parameters对象, seasonalParameter parameters字符串, version字符串和forecast数组。 So you should define a POJO which has those fields. 因此,您应该定义一个包含这些字段的POJO。
  2. station is an array which has one object and that object has id , websiteId , name , etc fields. station是一个具有一个对象的数组,该对象具有idwebsiteIdname等字段。 So you should define a POJO for station which has those fields. 因此,您应该为具有这些字段的工作站定义一个POJO。
  3. parameters is an object which has ozone , pm25 , temperature , wind_speed and wind_dir fields. parameters是一个具有ozonepm25temperaturewind_speedwind_dir字段的对象。 So you should define a POJO for which has those fields. 因此,您应该定义一个具有这些字段的POJO。
  4. ozone and pm25 has same fields, so they should be same class's instances. ozonepm25具有相同的场,因此它们应该是同一类的实例。 You should define a POJO for them. 您应该为它们定义一个POJO。

Now lets see our POJOS: 1) Your first level POJO: 现在让我们看一下我们的POJOS:1)您的第一级POJO:

public class Holder {
    private Station[] station;
    private Parameters parameters;
    private String seasonalParameter;
    private String version;
    private Object[] forecast;
}

2) Station POJO: 2)POJO站:

public class Station {
    private String id;
    private String websiteId;
    private String name;
    private String label;
    private String latitude;
    private String longitude;
    private String zip;
    private String region; 
}

3) POJO for parameters field: 3)POJO用于​​参数字段:

public class Parameters {
    private Param ozone;
    private Param pm25;
    private double temperature;
    private double wind_speed;
    private String wind_dir;
}

4) POJO for both ozone and pm25 fields: 4)适用于臭氧和pm25领域的POJO:

public class Param {
    private String value;
    private String unit;
    private String color;
    private String colorHex;
    private String arrow;
}

Test: 测试:

String json = "{\"station\":[{\"id\":\"p2\",\"websiteId\":\"p2\",\"name\":\"\",\"label\":\"Price\",\"latitude\":\"\",\"longitude\":\"\",\"zip\":\"\",\"region\":\"South\"}],\"parameters\":{\"ozone\":{\"value\":\"0.044\",\"unit\":\" ppm\",\"color\":\"17,250,11\",\"colorHex\":\"11fa11\",\"arrow\":\"unchange_gray\"},\"pm25\":{\"value\":\"NA\",\"unit\":\"\",\"color\":\"000000\",\"colorHex\":\"000000\",\"arrow\":\"\"},\"temperature\":40.4,\"wind_speed\":4.5,\"wind_dir\":\"SE\"},\"seasonalParameter\":\"ozone\",\"forecast\":[],\"version\":\"1.5\"}";
Gson gson = new Gson();
Holder item = gson.fromJson(json, Holder.class);

Note: Json sample you shared is not a valid json. 注意:您共享的Json示例不是有效的json。 "name": ,"latitude": ,"longitude": , are changed to "name": "","latitude": "","longitude": "" to make your json valid. “ name”:,“ latitude”:,“ longitude”:,更改为“ name”:“”,“ latitude”:“”,“ longitude”:“”使您的json有效。

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

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