I've been able to get a jsonarray from a json string, but don't know how to put it in a Hashmap with a String that shows the type of cargo and an Integer showing the amount.
The string:
"cargo":[
{"type":"Coals","amount":75309},
{"type":"Chemicals","amount":54454},
{"type":"Food","amount":31659},
{"type":"Oil","amount":18378}
]
Run Code Online (Sandbox Code Playgroud)
这为我解决了:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19690 次 |
| 最近记录: |