Although it isn't an invalid thing to have duplicate keys in a JSON, it is not recommended. Refer to this question and specifically this answer which quotes the RFC which recommends why to not have duplicates.
Given this, there a bit of a cumbersome way to add the entries into the Multimap
. The below codes assumes that the Json object you get is in the single line String
format. If not, it isn't difficult to convert into one (careful with the newlines, the code below doesnt take care of that).
String jsonStr = new String("{\"LCD\": \"Samsung\",\"MOUSE\": \"HP\",\"MOUSE\": \"DELL\",\"LCD\": \"Apple\",\"LCD\": \"DELL\",\"DRINK\": \"Coke\",\"LCD\": \"Lenovo\",\"DRINK\": \"Pepsi\",\"KEYBOARD\": \"Lenovo\"}");
// Removing the first and last braces
jsonStr = jsonStr.substring(1, jsonStr.length() - 1);
// Create a Guava Multimap
Multimap<String, String> myMap = ArrayListMultimap.create();
// Split on comma for each key-value pair
for(String item: jsonStr.split(",")) {
// Get the individual key-value pair
String[] keyValue = item.split(":");
// Get the values between the the inverted commas
String key = keyValue[0].substring(keyValue[0].indexOf("\"") + 1, keyValue[0].lastIndexOf("\""));
String value = keyValue[1].substring(keyValue[1].indexOf("\"") + 1, keyValue[1].lastIndexOf("\""));
// Add to map
myMap.put(key, value);
}
// Print to check
for(Map.Entry<String, String> entry: myMap.entries()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
This isn't the best way to achieve the outcome, but given the nature of the input JSON there isn't much that can be done, since most of the JSON parsers (Gson, Jackson) dont take care of the duplicates. The above code will give the following output (the final print statement for checking the entries)
DRINK: Coke
DRINK: Pepsi
MOUSE: HP
MOUSE: DELL
KEYBOARD: Lenovo
LCD: Samsung
LCD: Apple
LCD: DELL
LCD: Lenovo
Note that the order isn't (cannot be) maintained by this map.