【问题标题】:How can I get last element of JSON Object without specifying key value?如何在不指定键值的情况下获取 JSON 对象的最后一个元素?
【发布时间】:2021-11-13 07:39:48
【问题描述】:

这是来自该 API 的 covid 19 的数据:https://data.covid19india.org/v4/min/timeseries.min.json 我想到达 JAVA / Android Studio 中 "dates" JSON 对象的最后一个元素/或 "2020-03-06"

如果有人可以建议我如何在不将其转换或更改为 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 对象转换为 Array,然后返回最后一个元素。

标签: android json android-studio object jsonparser


【解决方案1】:

首先获取日期的 JSONObject,然后通过在其名称(键)上获取 JSONArray 来获取最后一项,因为您现在拥有其键数组,您可以获取存在于 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
    }
 }
}
}
}

【讨论】:

    【解决方案2】:

    我找到了解决这个问题的新方法,或者访问最后日期,查看它或者可以应用到你的代码中,它适用于我的项目,也适用于你的项目。

    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);
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2011-07-04
      • 1970-01-01
      • 2013-04-22
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多