Home > other >  How to access nested Json objects in Kotlin through Wordpress REST API?
How to access nested Json objects in Kotlin through Wordpress REST API?

Time:05-03

I am building an app using Kotlin for my WordPress blog, I want to access nested JSON object values from my WordPress blog, I can access the root id, userid.

Say I want to access the Title, that is inside rendered.

JSON

"id": 1826,
"date": "2022-04-18T15:02:49",
"date_gmt": "2022-04-18T15:02:49",
"guid": {
"rendered": "https://example.com/?p=1826"
},
"modified": "2022-04-18T15:02:51",
"modified_gmt": "2022-04-18T15:02:51",
"slug": "to-android-heres-how",
"status": "publish",
"type": "post",
"link": "https://example.com/news/to-android-heres-how/",
"title": {
"rendered": "To Android: Here’s How"
},

Here is my data class

package com.example.retrofitexample.dataclass

data class blogdata(
    val id: Int,
    val author: Int,
    val categories: List<Int>,
    val comment_status: String,
    val content: String,
    val date: String,
    val date_gmt: String,
    val excerpt: String,
.....
    val modified_gmt: String,
    val ping_status: String,
    val slug: String,
    val status: String,
    val sticky: Boolean,
    val template: String,
    val title: String,
  )

ActivityFile . kt

  private fun getMyData() {
                val retrofitBuilder = Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .build()
                    .create(APIinterface::class.java)

                val retrofitData = retrofitBuilder.getData()

                with(retrofitData) {

                    enqueue(object : Callback<List<blogdata>?> {
                        override fun onResponse(
                            call: Call<List<blogdata>?>,
                            response: Response<List<blogdata>?>
                        ) {
                           val myStringBilder = StringBuilder()
                          for (myData in responseBody) {
                               myStringBilder.append(myData.id)
                               myStringBilder.append("\n")
                          }

                            val txtID = findViewById<TextView>(R.id.userId)
                         txtID.text = myStringBilder

                        }


                        override fun onFailure(call: Call<List<blogdata>?>, t: Throwable) {
                            Log.d(TAG, "onFailure:"   t.message)
                        }

I am able to access the parent object details but not the nested ones.

CodePudding user response:

Here the title is not a type of string

"title": {
    "rendered": "To Android: Here&#8217;s How"
}

To access nested data, you've to make a data class for the nested data itself and then you can assign that variable to be a type of this defined data class

Create a data class named Title (this name could be anything)

data class Title (
    val rendered: String
    )
    

Inside parent data class assign title to be a of type Title

data class blogdata(
    val id: Int,
    val author: Int,
    val categories: List<Int>,
    val comment_status: String,
    val content: String,
    val date: String,
    val date_gmt: String,
    val excerpt: String,
.....
    val modified_gmt: String,
    val ping_status: String,
    val slug: String,
    val status: String,
    val sticky: Boolean,
    val template: String,
    val title: Title,
  )

As you are using GsonConverter you may want to give different names to the variables of data class than the one defined in the Json, as Kotlin follows camelCase while Json follows snake_case, you can use @SerializedName to give a different name to the variable than their Json counterpart.

Like for comment_status, you can do this

@SerializedName("comment_status")
val commentStatus: String
  • Related