如何从Hashmap获取特定的字符串



我正在尝试从hashmap获取特定值。

我将JsonObject转换为hashMap

Map<String, Object> mapObj = new Gson().fromJson(
        jsonObject, new TypeToken<HashMap<String, Object>>() {}.getType());

现在都很棒。

当我尝试从mapobj获得价值时像这样

String strValue = (String) mapObj.get("estimate_time");

它给出了null

我的json是

https://api.myjson.com/bins/16sub7

我真的不知道该怎么办。

" estimate_time"不是有效的键。它包含在密钥"数据"的值中。

如果您美化了JSON值,您将能够更清楚地注意到密钥" estimate_time"的值包含在密钥"数据"的值中。

要获取" estected_time"的值

我以前从未真正使用过Gson,但是以下几行应该有效(或者至少,请让您了解如何解决该问题(

Map<String, Object> mapObj = new Gson().fromJson(
    jsonObject, new TypeToken<HashMap<String, Object>>() {}.getType());
LinkedTreeMap<String, Object> requiredMapObj = (LinkedTreeMap<String, Object>) mapObj.get("data");
String strValue = (String) requiredMapObj.get("estimate_time");
String numberValueAlone = strValue.replaceAll("[^0-9]", "");

(参考(

最新更新