简体   繁体   中英

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

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结构

Now you should define the POJO for this structure.

  1. You have station array, parameters object, seasonalParameter String, version String and forecast array at first level of your json. So you should define a POJO which has those fields.
  2. station is an array which has one object and that object has id , websiteId , name , etc fields. So you should define a POJO for station which has those fields.
  3. parameters is an object which has ozone , pm25 , temperature , wind_speed and wind_dir fields. So you should define a POJO for which has those fields.
  4. ozone and pm25 has same fields, so they should be same class's instances. You should define a POJO for them.

Now lets see our POJOS: 1) Your first level POJO:

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

2) Station 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:

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:

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. "name": ,"latitude": ,"longitude": , are changed to "name": "","latitude": "","longitude": "" to make your json valid.

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