Home > Enterprise >  How to Parse json array as key value pair where position is key in android kotlin
How to Parse json array as key value pair where position is key in android kotlin

Time:08-30

I have json like below:

enter image description here

Here links as an array under values array. I have to get key and value from links array, like key will be 0 and value will be "http://www.example.com/....".

There may be multiple links in the links array like in 0 position, 1 position like that. How to get this as key pair?

I am trying in the below way:

dataBinding.itemUserControlTvPhone.setOnClickListener {
                    val failedQtyMapper = jacksonObjectMapper()
                    if (data?.get(position)?.links != null) {
                        var jsonStr = failedQtyMapper.writerWithDefaultPrettyPrinter()
                            .writeValueAsString(
                                data?.get(position)?.links ?: ""
                            )
                        Log.e("jsonString:=", jsonStr)

but getting jsonString as 
    E/jsonString= ["http://www.example.com/reports/ Place Tp 2021-08-17 19_16_29_440.csv"]

But I need this jsonString as a key value pair. How to do this?

CodePudding user response:

Since links is a list, each item in it will have an index from 0 to list.size - 1

You can do something like

   links?.forEachIndexed { index, item ->
        var jsonStr = "$index: $item"
        Log.e("jsonString:=", jsonStr)
    }
  • Related