android - 如何使用 Retrofit 2.0 (Kotlin) 正确解析嵌套的 JSON 对象?

标签 android json kotlin retrofit pojo

以下 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?
)

我该如何解析它? 谢谢。

最佳答案

你应该为“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>
    )

关于android - 如何使用 Retrofit 2.0 (Kotlin) 正确解析嵌套的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55067977/

相关文章:

android - EditText 显示 2 行文本,而不是 1 行,尽管设置了所有属性(仅在 Galax Tab 10.1,ICS 上)

Android开发-布局xml文件的eclipse ide中的 'missing theme error'

java - 如何解析服务器的 JSON 响应并进行比较

JavaScript:用 json 中的月份名称替换月份数字

json - 创建索引模板时,JSON解析异常

java - Android如何在Java中使用Kotlin-Coroutine?

android - Kotlin 函数需要 Nothing but defined as a different type

java - 使用 Itext 以 PDF 形式显示 SQLite 数据库的结果

javascript - express.js 在终端中获取 json 文件

java - 阻止 Android 中某些选定的菜单点以进行家长控制