11

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}
]
4
  • possible this link can help stackoverflow.com/questions/4307118/jsonarray-to-hashmap Commented Nov 8, 2015 at 16:05
  • no sorry, already tried that one, some methods don't seem to work with that example Commented Nov 8, 2015 at 16:17
  • what did you try and what was the result ? Commented Nov 8, 2015 at 16:45
  • I tried the code from the link you gave me. But "JSONObject" and "optJSONObject" couldn't be resolved, maybe because I use javax.json libraries and that guy uses another library? Commented Nov 10, 2015 at 9:53

4 Answers 4

8

This fixed it for me:

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Error @getJsonObject method
should be length() instead of size()?
How is this going to work? The last item of type and amount will win, and I don't see how the list is going to be created on the map. I think it should be defined as Map<String, ArrayList> cargList = new HasMap<>() then generate the array list myCargoList from the JSON Array, and add it to the map using cargo.put("cargoList", myCargoList). Please let me know if this is the correct understanding.
0

I've been able to solve this using the Google GSON

    Map<String, Integer> cargoMap = new HashMap<>();

    JsonObject jsonObject = new Gson().fromJson(yourJsonString, JsonObject.class);
    JsonArray cargo = jsonObject.getAsJsonArray("cargo");
    cargo.forEach(item ->
    {
        String type = item.getAsJsonObject().get("type").getAsString();
        int amount = item.getAsJsonObject().get("amount").getAsInt();
        if (StringUtils.isNotBlank(type) || amount != 0) {
            cargoMap.put(type, amount);
        }
    });

Where "yourJsonString" is the whole json that contains the cargo json array.

Comments

0

If the JSON is

    [
        {
            "key": "customerRefNo",
            "value": "A"
        }
    ]
   

 Map<String, String> map = new HashMap<>();
        params.forEach(e -> map.put(e.get("key").asText(), e.get("value").asText()));

Comments

-1

Try creating a new HashMap, and loop through the JSONArray, adding each element to the hashmap.

JSONArray allCargo = jsonObject.getJsonArray("cargo");

HashMap<String, String> hm = new HashMap();

for(Object cargo : allCargo) {
    LinkedHashMap lhm = (LinkedHashMap) cargo;
    hm.put((String)lhm.get("type"), (String)lhm.get("amount"));
}

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.