如何在不指定键值的情况下获取 JSON 对象的最后一个元素?
How can I get last element of JSON Object without specifying key value?
这是来自 API 的 covid 19 数据:https://data.covid19india.org/v4/min/timeseries.min.json
我想到达最后一个元素/或 "2020-03-06" of "dates" JSON [=22] 中的对象=] / Android 工作室。
如果有人可以向我建议如何在不将其转换或更改为 JSON 数组的情况下到达此 JSON 对象的最后一个元素。
"TT": {
"dates": {
"2020-01-30": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 1
},
"total": {
"confirmed": 1
}
},
"2020-02-02": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 2
},
"total": {
"confirmed": 2
}
},
"2020-03-03": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 3
},
"total": {
"confirmed": 6,
"recovered": 3
}
},
"2020-03-04": {
"delta": {
"confirmed": 22
},
"delta7": {
"confirmed": 25
},
"total": {
"confirmed": 28,
"recovered": 3
}
},
"2020-03-05": {
"delta": {
"confirmed": 2
},
"delta7": {
"confirmed": 27
},
"total": {
"confirmed": 30,
"recovered": 3
}
},
"2020-03-06": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 28
},
"total": {
"confirmed": 31,
"recovered": 3
}
}
首先获取日期的 JSON 对象,然后通过在其名称(键)上获取 JSON 数组来获取最后一个项目,因为您现在拥有其键的数组,您可以获取存在于 arr.length()-1
.
的最后一项
像这样:
JSONObject dates = jsonObject.getJSONObject("TT").getJSONObject("dates");
JSONArray arr = dates.names();
String lastDate = arr2.getString(arr.length()-1);
从那里你可以做任何事情,因为你有需要的钥匙。
顺便说一下,您的 JSON 缺少一些括号,这让我困扰了一段时间
{"TT": {
"dates": {
"2020-01-30": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 1
},
"total": {
"confirmed": 1
}
},
"2020-02-02": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 2
},
"total": {
"confirmed": 2
}
},
"2020-03-03": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 3
},
"total": {
"confirmed": 6,
"recovered": 3
}
},
"2020-03-04": {
"delta": {
"confirmed": 22
},
"delta7": {
"confirmed": 25
},
"total": {
"confirmed": 28,
"recovered": 3
}
},
"2020-03-05": {
"delta": {
"confirmed": 2
},
"delta7": {
"confirmed": 27
},
"total": {
"confirmed": 30,
"recovered": 3
}
},
"2020-03-06": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 28
},
"total": {
"confirmed": 31,
"recovered": 3
}
}
}
}
}
我找到了解决此问题的新方法,或访问最后日期,查看它或可以在您的代码中应用,它适用于我的项目,也适用于您的项目。
JSONObject tempData = response.getJSONObject("TT").getJSONObject("dates");
JSONObject lastElement = tempData.getJSONObject(String.valueOf(tempData.names().get(tempData.length() - 1)));
String date = key1.substring(8,10)+ "/" + key1.substring(5,7) + "/" + key1.substring(0,4);
这是来自 API 的 covid 19 数据:https://data.covid19india.org/v4/min/timeseries.min.json 我想到达最后一个元素/或 "2020-03-06" of "dates" JSON [=22] 中的对象=] / Android 工作室。
如果有人可以向我建议如何在不将其转换或更改为 JSON 数组的情况下到达此 JSON 对象的最后一个元素。
"TT": {
"dates": {
"2020-01-30": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 1
},
"total": {
"confirmed": 1
}
},
"2020-02-02": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 2
},
"total": {
"confirmed": 2
}
},
"2020-03-03": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 3
},
"total": {
"confirmed": 6,
"recovered": 3
}
},
"2020-03-04": {
"delta": {
"confirmed": 22
},
"delta7": {
"confirmed": 25
},
"total": {
"confirmed": 28,
"recovered": 3
}
},
"2020-03-05": {
"delta": {
"confirmed": 2
},
"delta7": {
"confirmed": 27
},
"total": {
"confirmed": 30,
"recovered": 3
}
},
"2020-03-06": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 28
},
"total": {
"confirmed": 31,
"recovered": 3
}
}
首先获取日期的 JSON 对象,然后通过在其名称(键)上获取 JSON 数组来获取最后一个项目,因为您现在拥有其键的数组,您可以获取存在于 arr.length()-1
.
的最后一项
像这样:
JSONObject dates = jsonObject.getJSONObject("TT").getJSONObject("dates");
JSONArray arr = dates.names();
String lastDate = arr2.getString(arr.length()-1);
从那里你可以做任何事情,因为你有需要的钥匙。
顺便说一下,您的 JSON 缺少一些括号,这让我困扰了一段时间
{"TT": {
"dates": {
"2020-01-30": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 1
},
"total": {
"confirmed": 1
}
},
"2020-02-02": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 2
},
"total": {
"confirmed": 2
}
},
"2020-03-03": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 3
},
"total": {
"confirmed": 6,
"recovered": 3
}
},
"2020-03-04": {
"delta": {
"confirmed": 22
},
"delta7": {
"confirmed": 25
},
"total": {
"confirmed": 28,
"recovered": 3
}
},
"2020-03-05": {
"delta": {
"confirmed": 2
},
"delta7": {
"confirmed": 27
},
"total": {
"confirmed": 30,
"recovered": 3
}
},
"2020-03-06": {
"delta": {
"confirmed": 1
},
"delta7": {
"confirmed": 28
},
"total": {
"confirmed": 31,
"recovered": 3
}
}
}
}
}
我找到了解决此问题的新方法,或访问最后日期,查看它或可以在您的代码中应用,它适用于我的项目,也适用于您的项目。
JSONObject tempData = response.getJSONObject("TT").getJSONObject("dates");
JSONObject lastElement = tempData.getJSONObject(String.valueOf(tempData.names().get(tempData.length() - 1)));
String date = key1.substring(8,10)+ "/" + key1.substring(5,7) + "/" + key1.substring(0,4);