简体   繁体   English

如何从 retrofit 响应中获取嵌套的 JSON object 和数组?

[英]How to get nested JSON object and array from retrofit response on Android?

What I am trying to do is get the nested JSON object and array form the web response using retrofit我想要做的是使用 ZBD279364F96CA5CBCAB793 得到嵌套的 JSON object 和数组形成 web 响应

the response is:回应是:

{
"user": [
    {          
      "name": "Kaleigh Stamm",          
      "post": {
        "id": 1234,            
        "link": [
          "https://www.link1.com"
          "https://www.link2.com"
          "https://www.link3.com"
        ]
      }
    }
}

I can get the name with no problem, but I have not been able to parse the post object neither the link array which could be null or have several links.我可以毫无问题地获得名称,但我无法解析 object 帖子,也无法解析可能是 null 或具有多个链接的链接数组。 I now that some post class needs to be implemented but do not know quite how to do it我现在需要执行一些帖子 class 但不知道该怎么做

I have a user class我有一个用户 class

class user {
    
    @SerializedName("name")
    var name = ""
    
}

I have an interface我有一个界面

interface userService {
    @GET("baseurlinfo")
    fun getUserList() : Call<userResponse>
}

And this is my main这是我的主要

var BaseUrl = "https://baseURL.com/"
    val retrofit = Retrofit.Builder()
            .baseUrl(BaseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    val service = retrofit.create(userService::class.java)

    val call = service.getUserList()
    call.enqueue(object : Callback<userResponse> {
        override fun onResponse(call: Call<userResponse>, response: Response<userResponse>) {
            if (response.code() == 200) {
                val userResponse = response.body()!!
                for( user in userResponse.user){
                    Log.v("MainActivity", user.name)
                }
            }
        }
        override fun onFailure(call: Call<userResponse>, t: Throwable) {
            Log.v("MainActivity", t.toString())
        }
    })

Any help or suggestion on how to process the rest of the JSON response would be great, thanks.任何有关如何处理 JSON 响应的 rest 的帮助或建议都会非常好,谢谢。

Your user class is incomplete.您的用户 class 不完整。 First add below JSON in this site ( json to pojo ).首先在本站的 JSON 下方添加( json 到 pojo )。 then get java class and then paste it into android studio.然后得到 java class 然后粘贴到 android studio。 Then android studio automatically converts it to kotlin class.然后android studio自动转换为kotlin class。 then simply get your links from the created user class.然后只需从创建的用户 class 获取您的链接。 also, your JSON format was in incorrect format and should be like below:此外,您的 JSON 格式不正确,应如下所示:

{
"user": [
{          
  "name": "Kaleigh Stamm",          
  "post": {
    "id": 1234,            
    "link": [
      "https://www.link1.com",
      "https://www.link2.com",
      "https://www.link3.com"
    ]
  }
}
]
}

create a post class and call it from the user class创建一个帖子 class 并从用户 class 调用它

class userPost {
    @SerializedName("id")
    private var mId: Int? = null

@SerializedName("link")
private var mLink: ArrayList<String>? = null

fun getId(): Int {
    return mId!!
}

fun setId(id: Int) {
    mId = id
}

fun getLink(): ArrayList<String> {
    return mLink!!
}

fun setLink(link: ArrayList<String>) {
    mLink= link
}

}

And on the user class add并在用户 class 添加

@SerializedName("post")
    private var mPost: userPost? = null

    fun getPost(): userPost? {
        return mPost
    }

    fun setPost(post: userPost) {
        mPost = post
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从改型响应中获取JSON ARRAY和JSON对象响应? - How to get JSON ARRAY and JSON object response from retrofit response? Retrofit 2如何从嵌套的json对象获取响应(android) - Retrofit 2 how to get the response from nested json objects (android) 改造-如何从json对象获取响应 - retrofit - how to get response from json object 如何使用 retrofit android 发布请求和获取响应(嵌套 JSON) - How to both post request and get response (nested JSON) with retrofit android Retrofit如何构建响应类以从列表嵌套对象json获取数据 - Retrofit How to build response class to get data from list nested object json Android 如何从 retrofit 获取 json 数组 - Android how to get json array from retrofit 如何在Android中使用改造在Json响应中获取数组数据? - How to get array data in Json response using retrofit in android? How to Get Json object and Json Array in Same KEY by retrofit for android - How to Get Json object and Json Array in Same KEY by retrofit for android 如何从android(java)中的json响应中获取内部索引对象,该对象对rest api调用使用改造 - How to get inner indexed object from json response in android (java) which use retrofit for rest api calls 如何使用视图绑定android从嵌套的JSON数组中获取Retrofit中的数据 - How to get data in Retrofit from nested JSON array using view binding android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM