加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何将JsonArray转换为Hashmap

发布时间:2020-12-16 18:40:14 所属栏目:百科 来源:网络整理
导读:我已经能够从json字符串中获取jsonarray,但是不知道如何将它放在带有显示货物类型的String的Hashmap和显示金额的Integer中. 字符串: "cargo":[ {"type":"Coals","amount":75309},{"type":"Chemicals","amount":54454},{"type":"Food","amount":31659},{"typ
我已经能够从json字符串中获取jsonarray,但是不知道如何将它放在带有显示货物类型的String的Hashmap和显示金额的Integer中.

字符串:

"cargo":[   
    {"type":"Coals","amount":75309},{"type":"Chemicals","amount":54454},{"type":"Food","amount":31659},{"type":"Oil","amount":18378}
]

解决方法

这为我修好了:

JsonArray jsoncargo = jsonObject.getJsonArray("cargo");

Map<String,Integer> cargo = new HashMap<>();
for (int i = 0; i < jsoncargo.size(); i++) {            
    String type = jsoncargo.getJsonObject(i).getString("type");
    Integer amount = jsoncargo.getJsonObject(i).getInt("amount");
    cargo.put(type,amount);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读