【发布时间】:2021-04-08 13:40:06
【问题描述】:
我正在尝试从 Json 文件中提取对象列表,但是该文件以一个对象开头,该对象包含我需要的对象数组。我是新来改造这里是我的文件: 接口
public interface OSMApi {
@Headers({"Accept: application/json"})
@GET("map")
Call<List<OSMObject>> getOSMObjects(@Query("bbox") String boxMinMaxLatLon);
}
控制器
public class OSMController implements Callback<List<OSMObject>> {
static final String BASE_URL = "https://api.openstreetmap.org/api/0.6/";
private static final String TAG = "OSMController";
private String bboxLatLon = "//latlongshere";
public void start(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
OSMApi osmApi = retrofit.create(OSMApi.class);
Call<List<OSMObject>> call = osmApi.getOSMNodes(bboxLatLon);
call.enqueue(this);
}
@Override
public void onResponse(Call<List<OSMObject>> call, Response<List<OSMObject>> response) {
if(response.isSuccessful()){
List<OSMObject> OSMObjectList = response.body();
OSMObjectList.forEach(OSMObject -> Log.d(TAG, "onResponse: objects "+OSMObject.getId()));
}else{
Log.d(TAG, "onResponse: here : "+response.errorBody() + ", "+response.code());
}
}
@Override
public void onFailure(Call<List<OSMObject>> call, Throwable t) {
Log.d(TAG, "onFailure: "+ t.getStackTrace() + ", extra: "+ t.getMessage());
}
}
Json 的开头
{
"version": "0.6",
"generator": "CGImap 0.8.3 (652902 spike-07.openstreetmap.org)",
"copyright": "OpenStreetMap and contributors",
"attribution": "http://www.openstreetmap.org/copyright",
"license": "http://opendatacommons.org/licenses/odbl/1-0/",
"bounds": {
"minlat": 1.0,
"minlon": 1.0,
"maxlat": 1.0,
"maxlon": 1.0
},
"elements": [
{
"type": "node",
"id": 443479273,
"lat": 1.0,
"lon": 1.0,
"timestamp": "2011-04-11T18:06:02Z",
"version": 3,
"changeset": 4435725,
"user": "BeerOfTheTrip",
"uid": 440880
},
{
"type": "node",
"id": 443479278,
"lat": 1.0,
"lon": 1.0,
"timestamp": "2011-04-11T18:06:01Z",
"version": 2,
"changeset": 4435725,
"user": "BeerOfTheTrip",
"uid": 440880
},
{
"type": "node",
"id": 443479281,
"lat": 1.0,
"lon": 1.0,
"timestamp": "2011-04-11T18:06:01Z",
"version": 2,
"changeset": 4435725,
"user": "BeerOfTheTrip",
"uid": 440880
},
etc..........
我的对象Pojo
public class OSMObject implements SerializedName {
@SerializedName("type")
@Expose
private String type;
@SerializedName("id")
@Expose
private int id;
@SerializedName("lat")
@Expose
private double lat;
@SerializedName("lon")
@Expose
private double lon;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("version")
@Expose
private int version;
@SerializedName("changeset")
@Expose
private int changeset;
@SerializedName("uid")
@Expose
private int uid;
@SerializedName("nodes")
@Expose
private int[] nodes;
//Separation
private ArrayList<OSMNode> OSMNodes = new ArrayList<>();
private ArrayList<OSMWay> OSMWays = new ArrayList<>();
public OSMObject(String type, int id, String timestamp, int version, int changeset, int uid, int[] nodes) {
this.type = type;
this.id = id;
this.timestamp = timestamp;
this.version = version;
this.changeset = changeset;
this.uid = uid;
this.nodes = nodes;
}
public OSMObject(String type, int id, double lat, double lon, String timestamp, int version, int changeset, int uid) {
this.type = type;
this.id = id;
this.lat = lat;
this.lon = lon;
this.timestamp = timestamp;
this.version = version;
this.changeset = changeset;
this.uid = uid;
}
+getters and setters....
所以我不需要 jsonObject 中的任何初始信息,我只需要 Elements jsonArray 中的对象列表,这由两个略有不同的对象组成,一个有 Nodes[] 一个没有。因此 pojo 中的 2 个构造函数?
如何设置改造来处理这个问题?目前我遇到的错误是:
onFailure: [Ljava.lang.StackTraceElement;@dbe860f, extra: 预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处是 BEGIN_OBJECT
补充: 两个不同对象的示例:
{
"type": "node",
"id": 4448646916,
"lat": 1.0,
"lon": 1.0,
"timestamp": "2020-12-21T17:52:35Z",
"version": 1,
"changeset": 44211151,
"user": "Mike Greenwood",
"uid": 44623152
},
{
"type": "way",
"id": 44117969,
"timestamp": "2014-04-11T14:01:16Z",
"version": 2,
"changeset": 44627679,
"user": "Smurfy12",
"uid": 44220096,
"nodes": [
4483345702,
443479293,
443479273
],
"tags": {
"highway": "residential",
"name": "A Street"
}
},
正如您所看到的,它们具有不同的“类型”,一个“节点”和一个“方式”,方式有一个节点数组,而节点有一个“纬度”和“经度”,我不需要担心现在的“标签”。
【问题讨论】: