首页 > 解决方案 > 获取 JSON 中的数组列表

问题描述

我的 JSON 看起来像这样

{"status":true,"error_message":[],"locations":[{"id":"12","name":"office"},{"id":"13","name":"home"}]}

我正在使用协程和改造与 Web 服务进行通信。我想在arrayList 中获取office和,但我只能获取.homelocationsoffice

fun getLocation(): String {
                val service = RetrofitFactory.makeRetrofitService()
                GlobalScope.launch(Dispatchers.Main) {
                    val request = WebApi.getLocationList(context)
                    request?.locations.let {
                        for (i in it!!.iterator()) {
                           longToast(request?.locations!!.size.toString()) 

longToast(request?.locations!!.component1().name.toString())
                        }
                    }
                }           
                return "sss"
            }

输出

2
office
2
office

期望的输出

2
offine
2
home

标签: androidjsonkotlinretrofitcoroutine

解决方案


您的吐司不使用ifor 循环。尝试切换到这个: longToast(i.component1().name.toString())


推荐阅读