为了简化结构,我使用了json字符串:
{
"route": {
"bus-1": {
"stations": [
],
"geo": [
]
},
"bus-2": {
"stations": [
],
"geo": [
]
}
},
"routesReverse": {
"bus-1": {
"stations": [
],
"geo": [
]
},
"bus-2": {
"stations": [
],
"geo": [
]
}
}
}我试着用GSON解析它
public class MainJson {
@SerializedName("route")
@Expose
public Routes route;
@SerializedName("routesReverse")
@Expose
public Routes routesReverse;
public Routes getRoute() {
return route;
}
public Routes getRoutesReverse() {
return routesReverse;
}
}我已经创建了所有的模型,但是我有一个关于这个模型的问题:
public class Routes {
@SerializedName("bus-1")
@Expose
BusStop busStop1;
@SerializedName("bus-2")
@Expose
BusStop busStop2;
public BusStop getBusStop1() {
return busStop1;
}
public BusStop getBusStop2() {
return busStop2;
}
}我不喜欢用这种方法为每个总线路径创建带有注释的BusStop对象,我想创建类似于List<BusStop>的东西,因为我的json只有2条路径。
如何做到这一点?
发布于 2016-10-22 16:07:52
您能修改您收到的json的结构吗?因为最简单的方法是有一个名为"bus“的数组,而不是在”have“json对象中的多个”bus“对象。如果您不能修改json,那么我认为GSON内部没有任何方便的解决方案,因为它可以映射对象1到1,即使您应用了“备用”标记。查看@SerializedName注释这里的文档。
发布于 2016-10-22 16:08:00
//Each object is just a list of bus stops
public class MainJson {
@Expose
public List<BusStop> route;
@Expose
public List<BusStop> routesReverse;
public List<BusStop> getRoute() {
return route;
}
public List<BusStop> getRoutesReverse() {
return routesReverse;
}
}
public class BusStop {
@Expose
List<Object> stations;
@Expose
List<Object> geo;
public List<Object> getStations() {
return stations;
}
public List<Object> getGeo() {
return geo;
}
}还不清楚哪个站点/geo包含哪些元素,但由于您使用了数组表示法,我假设它们每个都包含一个对象列表。
https://stackoverflow.com/questions/40193990
复制相似问题