android - 获取 JSON 中的数组列表
问题描述
我的 JSON 看起来像这样
{"status":true,"error_message":[],"locations":[{"id":"12","name":"office"},{"id":"13","name":"home"}]}
我正在使用协程和改造与 Web 服务进行通信。我想在arrayList 中获取office
和,但我只能获取.home
locations
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
解决方案
您的吐司不使用i
for 循环。尝试切换到这个:
longToast(i.component1().name.toString())
推荐阅读
- python - 我想收集令牌的计数。看看最常见的标记是什么?,我写的代码不起作用,所以我评论了我的代码
- c++ - C ++设置和获取方法不产生值?
- flutter - Flutter Error FAILURE:构建失败并出现异常
- core-data - 警告:尝试在已经呈现(空)SwiftUI 的 * 上呈现 *
- php - 基于行数的透视数组
- swift - Swift 结合发布者与完成处理程序以及何时取消
- xcode - 工作区完整性,无法加载项目。升级到 MACOS Catalina 后
- javascript - 如何连接到 Firebase Firestore?
- flutter - Flutter:区分 Widget param = null 和未设置的参数
- redis - Redis 是否支持具有多个节点的哨兵设置中的数据库?