【问题标题】:Get arraylist in JSON获取 JSON 中的数组列表
【发布时间】:2019-06-23 11:07:44
【问题描述】:

我的 JSON 看起来像这样

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

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

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

【问题讨论】:

    标签: android json kotlin retrofit coroutine


    【解决方案1】:

    您的 toast 没有使用 for 循环中的 i。尝试切换到这个: longToast(i.component1().name.toString())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2021-11-01
      • 1970-01-01
      • 2019-01-15
      • 2023-01-29
      • 2021-03-25
      • 2020-12-05
      相关资源
      最近更新 更多