0

I want to call a post API whose parameters is an array of objects eg:

{
"articles":[
    {
    "mobile":85634293205468,
    "name":"Name1"
    },
    {
    "mobile":9535934854445,
    "name":"Name2"
    },
    {
        "mobile":589524452264,
        "name":"Name3"
    }
    ],
"user_id":143324757
}

is there a way to make a variable of array of objects type so that I can call for this API. I am a beginner in android please tell me the reason if the question is not clear.

Thank you

1
  • 1
    You need a serialization library to convert JSON into classes. Such as GSON or the kotlinx.serialization library. You can look up instructions for using them on their official sites. Commented Jan 13, 2022 at 20:03

1 Answer 1

1

There is a handy JSON to Kotlin converter, which converts json to this classes:

data class UserModel (

  @SerializedName("articles" ) var articles : ArrayList<Articles> = arrayListOf(),
  @SerializedName("user_id"  ) var userId   : Int?                = null

)

data class Articles (

  @SerializedName("mobile" ) var mobile : Int?    = null,
  @SerializedName("name"   ) var name   : String? = null

)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please tell me how could I use it with volley library.
I don't know how you are making requests, but there is gson library, which can be used to convert classes to json string, and vice versa. github.com/google/gson/blob/master/UserGuide.md

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.