Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access nested json object in kotlin

This is my array where I’m trying to access videoId inside id but unable to get any nested object after id. How can I achieve nested object inside this json

 "items": [
        {
          "kind": "youtube#searchResult",
          "etag": "nbjIRj0V6P8db9BnvCTC0aBektc",
          "id": {
            "kind": "youtube#video",
            "videoId": "ByTQuYms2Sk"
          },
          "snippet": {
            "publishedAt": "2021-03-01T15:05:57Z",
            "channelId": "UCHWfAuT1j7bTLXTIBcY_l6w",
            "title": "[TOP 100] MOST VIEWED K-POP SONGS OF ALL TIME • MARCH 2021",
            "description": "Welcome back to the Top 100 Most Viewed #KPOP Songs of All Time! We have been updating you on views for over 6 years now!",
            "thumbnails": {
              "default": {
                "url": "https://i.ytimg.com/vi/ByTQuYms2Sk/default.jpg",
                "width": 120,
                "height": 90
              },
              "medium": {
                "url": "https://i.ytimg.com/vi/ByTQuYms2Sk/mqdefault.jpg",
                "width": 320,
                "height": 180
              },
              "high": {
                "url": "https://i.ytimg.com/vi/ByTQuYms2Sk/hqdefault.jpg",
                "width": 480,
                "height": 360
              }
            },
            "channelTitle": "K-Ville Entertainment",
            "liveBroadcastContent": "none",
            "publishTime": "2021-03-01T15:05:57Z"
          }
        }

I'm trying to access nested json object id and videoId inside json tree. This is what I've tried so far


response ->
                try {
                    val jsonArray = response.getJSONArray("items")
                    for (i in 1..jsonArray.length()) {
                        val jsonObject = jsonArray.getJSONObject(i)
                        val id = jsonObject.getString("id")
                        Log.i(TAG, "parseJson: $id")
                    }
                } catch (e: Exception) {
                    Log.i(TAG, "parseJson: ${e.message}")
                }
            }

but it does not target videoId inside id. Kindly guide me how can I achieve this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

response ->
            try {
                val jsonArray = response.getJSONArray("items")
                for (i in 1..jsonArray.length()) {
                    val jsonObject = jsonArray.getJSONObject(i)
                    // TRY THIS
                    val id = jsonObject.getJSONObject("id").getString("videoId")
                    Log.i(TAG, "parseJson: $id")
                }
            } catch (e: Exception) {
                Log.i(TAG, "parseJson: ${e.message}")
            }
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading