【发布时间】:2019-10-20 01:37:02
【问题描述】:
我正在尝试使用以下 kotlin 代码解析来自 json 的数据:
val text = JSONObject(URL(request).readText())
val results = text.getJSONArray("results")
val name = results.getJSONObject(5).getString("name") // org.json.JSONException: Index 5 out of range [0..1)
json
{
summary: {
queryType: "NEARBY",
queryTime: 13,
numResults: 2,
offset: 0,
totalResults: 2,
fuzzyLevel: 1,
geoBias: {
lat: -37.736343,
lon: 145.152114
}
},
results: [
{
type: "POI",
id: "AU/POI/p0/77255",
score: -0.38554,
dist: 385.5365152133808,
info: "search:ta:0323405846509-AU",
poi: {
name: "La Gourmet",
但是我在第 3 行收到以下错误:
org.json.JSONException: Index 5 out of range [0..1)
我不确定为什么会收到此错误。我求助于通过索引搜索name,因为.getJSONObject("poi") 不需要String。这也令人担忧,因为数据可能会发生变化,所以我更愿意通过String 查询JSON。
有什么想法吗?
【问题讨论】: