[英]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值的结构):
Now you should define the POJO for this structure. 现在,您应该为此结构定义POJO。
station
array, parameters
object, seasonalParameter
String, version
String and forecast
array at first level of your json. station
数组, parameters
对象, seasonalParameter
parameters
字符串, version
字符串和forecast
数组。 So you should define a POJO which has those fields. station
is an array which has one object and that object has id
, websiteId
, name
, etc fields. station
是一个具有一个对象的数组,该对象具有id
, websiteId
, name
等字段。 So you should define a POJO for station which has those fields. parameters
is an object which has ozone
, pm25
, temperature
, wind_speed
and wind_dir
fields. parameters
是一个具有ozone
, pm25
, temperature
, wind_speed
和wind_dir
字段的对象。 So you should define a POJO for which has those fields. ozone
and pm25
has same fields, so they should be same class's instances. ozone
和pm25
具有相同的场,因此它们应该是同一类的实例。 You should define a POJO for them. 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.