简体   繁体   中英

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

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. I now that some post class needs to be implemented but do not know quite how to do it

I have a user 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.

Your user class is incomplete. First add below JSON in this site ( json to pojo ). then get java class and then paste it into android studio. Then android studio automatically converts it to kotlin class. then simply get your links from the created user class. also, your JSON format was in incorrect format and should be like below:

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

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

    fun getPost(): userPost? {
        return mPost
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM