将GeoJson转换为mapbox多边形对象(Android)

问题描述 投票:0回答:1

我从服务器收到一个JSON响应,其中包含GeoJson格式的Polygon信息,如下所示:

{
"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]

我该如何解决这个问题?

java android gson mapbox
1个回答
0
投票

您的服务器的JSON响应结构与代码不符,因此出现上述错误。

根据POJO类Point(give here),JSON响应的结构应类似于:

{
   "status": "success",
   "location": {
                "type": "Polygon",
                "coordinates": [
                    [
                        { type: "String",
                          coordinates: [50.86541175842285,
                            35.61676761271693]
                        }
                    ]
                ]
            }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.