我有一个来自服务器的 json 响应,其中包含 GeoJson 格式的多边形信息,如下所示:
{
"status": "success",
"location": {
"type": "Polygon",
"coordinates": [
[
[
50.83657264709473,
35.602881307254144
],
[
50.86541175842285,
35.602881307254144
],
[
50.86541175842285,
35.61676761271693
],
[
50.83657264709473,
35.61676761271693
],
[
50.83657264709473,
35.602881307254144
]
]
]
}
}
现在我想将这个json转换为mapbox Polygon ,我正在使用带有 gson 转换器的改造来接收来自服务器的响应:
@GET("example.com")
Call<MyResponse> getAll();
import com.mapbox.geojson.Polygon;
public class MyResponse {
private String status;
private Polygon location;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Polygon getLocation() {
return location;
}
public void setLocation(Polygon location) {
this.location = location;
}
}
,但我有这个错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $.location.coordinates[0][0]
如何解决这个问题?
最佳答案
您的服务器的 JSON 响应结构与代码预期不符,因此出现上述错误。
根据 POJO 类 Point ( give here ),JSON 响应的结构应如下所示:
{
"status": "success",
"location": {
"type": "Polygon",
"coordinates": [
[
{ type: "String",
coordinates: [50.86541175842285,
35.61676761271693]
}
]
]
}
}
关于java - 将 GeoJson 转换为 mapbox Polygon 对象 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62228006/