检查 Json 数组是否为空或包含一个 Json 对象
Check if Json Array is empty or contains one Json Object
如何检查是否
{
"data": [
{
"latitude": 12,
"longitude": 13
}
]
}
包含一个对象,如上例,或者为空,如下例:
{
"data": [
[]
]
}
这是我的代码片段:
...
JSONObject jsonObject = new JSONObject(response.body());
JSONArray jsonArray = jsonObject.getJSONArray("data");
...
您可以像这样检查数据数组长度:
if(jsonArray.length() > 0) { //it means that there is a record in data attr }
如何检查是否
{
"data": [
{
"latitude": 12,
"longitude": 13
}
]
}
包含一个对象,如上例,或者为空,如下例:
{
"data": [
[]
]
}
这是我的代码片段:
...
JSONObject jsonObject = new JSONObject(response.body());
JSONArray jsonArray = jsonObject.getJSONArray("data");
...
您可以像这样检查数据数组长度:
if(jsonArray.length() > 0) { //it means that there is a record in data attr }