以下JSON对象是我从服务器接收的内容(获取请求)。我需要获取坐标值(纬度,经度)
{
"loc": {
"type": "Point",
"coordinates": [
-47.0487786,
-22.9001656
]
},
"city": "New Jersey",
"name": "John Doe",
"_id": "5c7958b3e3234b3472d9917d"
}
我正在尝试使用以下Poko(Kotlin):
package com.zowye.API.Models
import com.google.gson.annotations.SerializedName
class Salao
(
@SerializedName("loc") var coordinate: , // not sure about the type
var city: String?,
var name: String?
)
我该如何解析? 谢谢。
答案 0 :(得分:1)
再添加一个代表Location的类,代表测试对象的类型。
package com.zowye.API.Models
import com.google.gson.annotations.SerializedName
class Location (
var type: String?,
var coordinates: Float[]?
)
class Salao
(
@SerializedName("loc") var coordinate: Location,
var city: String?,
var name: String?
)
答案 1 :(得分:1)
您应该为“ loc”创建一个数据类
data class Salao(
@SerializedName("loc")
val location : Location,
val city : String,
val name : String,
@SerializedName("_id")
val id : String
)
data class Location (
val type : String,
val coordinates : Array<Float>
)