获取错误'org.json. JSON数组无法转换为com.google.android.gms.maps.model.LatLng Android

iszxjhcz  于 2023-02-02  发布在  Android
关注(0)|答案(1)|浏览(89)
    • Json响应:-**
{
"type" : "FeatureCollection",
"features": [
    {
        "geometry": {
            "coordinates": [ [ [ [
                -83.855020462486394,
                 36.800791838085125
            ], [
                -83.855122494686938,
                 36.800830858080204
            ], [
                -83.856076988974678,
                 36.801197800946333
            ], [
                -83.856471494201926,
                 36.801023860713194
            ], [
                -83.860159013544774,
                 36.799399831464761
            ]]]]
        }
    }
}
    • 代码:-**
val `object` = JSONObject(x)
val features = `object`.getJSONArray("features")
for (i in 0 until features.length()) {
val featureObject = features.getJSONObject(i).getJSONObject("geometry")
if (featureObject.getString("type") == "MultiPolygon") {
    Log.e("@@@@","length...."+features.length())
   }
}

for (j in 0 until ((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).length()) {

Log.e("coordinates@#@#", " "+((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).get(j))

multiplePolyTempList.add(((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).get(j) as Any)

Log.e("Anyy  ", "AnyyType ... "+((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).get(j) as Any)

}

Exceptionjava.lang.ClassCastException:JSON数组无法强制转换为com. google. android. gms. maps. model. LatLng
有人可以请解释给我如何转换org. json. jSONArray转换为com. google. android. gms. maps. model. latlng,我已经尝试过以上实现,但还没有结果。
任何帮助都将不胜感激。
先谢了。

mrwjdhj3

mrwjdhj31#

当最后一个JsonArray包含两个双精度值时,您可以通过提取这些双精度值并创建LatLng对象来将其转换为LatLng。例如,您可以使用以下帮助函数。

fun convertToLatLng(jsonArray: JSONArray): LatLng {
    val lat = jsonArray.getDouble(0)
    val lng = jsonArray.getDouble(1)
    return LatLng(lat, lng)
}

相关问题