Java 如何使用改型来解析具有不同大小对象和数组的JSON对象?
首先,我想说,我广泛搜索了如何做到这一点,使用了(至少)以下链接的建议: 但是,上面的所有链接都显示了如何使用相同类型的对象或一个数组中的对象解析JSON对象/数组 以下是我正在处理的特定JSON数据:Java 如何使用改型来解析具有不同大小对象和数组的JSON对象?,java,arrays,json,object,retrofit,Java,Arrays,Json,Object,Retrofit,首先,我想说,我广泛搜索了如何做到这一点,使用了(至少)以下链接的建议: 但是,上面的所有链接都显示了如何使用相同类型的对象或一个数组中的对象解析JSON对象/数组 以下是我正在处理的特定JSON数据: { "coord":{ "lon":-83.92, "lat":34.08 }, "weather":[ { "id":500, "main":"Rain", "description":"light rain",
{
"coord":{
"lon":-83.92,
"lat":34.08
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"light rain",
"icon":"10d"
}
],
"base":"stations",
"main":{
"temp":292.16,
"pressure":1011,
"humidity":64,
"temp_min":290.15,
"temp_max":294.15
},
"visibility":16093,
"wind":{
"speed":3.6,
"deg":290,
"gust":7.7
},
"clouds":{
"all":90
},
"dt":1524609300,
"sys":{
"type":1,
"id":809,
"message":0.0057,
"country":"US",
"sunrise":1524567171,
"sunset":1524615295
},
"id":420007383,
"name":"Athens",
"cod":200
}
我使用jsonschema2pojo.org将其全部转换为pojo。我的问题是,当我在JSON数组中已经有了一个“main”对象时,如何使用Reformation来访问Weather对象中的一个变量,如“main”?
就这一点而言,如何使用改装访问任何内部对象
我已经使所有变量都可序列化,并尝试对数组中的对象使用静态类
例如,假设我想在“weather”对象中检索“id”。我的JSONResponse类如下所示:
public class JSONResponse {
@SerializedName("weather")
@Expose
private List<Weather> weather = null;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("name")
@Expose
private String name;
//getter and setter methods
}
在我的主要活动中,我将如何使用改装检索这些数据?
如果只使用RXJava而不进行改造,我会更好吗?如果是这样的话,我该怎么做呢?
提前感谢所有有耐心回答这个问题的人
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("main")
@Expose
//This describes what the weather is like, e.g. clear or cloudy
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}