You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Kotlin Volley获取JSON中responseURL数组数据遇阻求助

解决Volley获取JSON数组字段的问题

你可以通过getJSONArray方法正确获取responseURL数组,再遍历数组提取每个URL字符串。以下是完整的Kotlin实现代码:

// 假设response是Volley返回的JSONObject对象
val responseURLArray = response.getJSONArray("responseURL")
val urlList = mutableListOf<String>()

// 遍历数组,逐个取出URL
for (i in 0 until responseURLArray.length()) {
    val url = responseURLArray.getString(i)
    urlList.add(url)
}

额外注意事项:

  • 确保字段名拼写完全匹配:JSON中的键是responseURL,大小写和符号都不能错,这是常见的出错点
  • 增加空值判断避免崩溃:如果API可能返回空数组或null字段,建议先做校验:
if (response.has("responseURL") && !response.isNull("responseURL")) {
    val responseURLArray = response.getJSONArray("responseURL")
    val urlList = mutableListOf<String>()
    for (i in 0 until responseURLArray.length()) {
        if (!responseURLArray.isNull(i)) {
            urlList.add(responseURLArray.getString(i))
        }
    }
    // 处理获取到的urlList
}

内容的提问来源于stack exchange,提问作者Matthias Röhrs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 21:27:05