I have below String which is in the format of key1=value1, key2=value2
which I need to load it in a map (Map<String, String>)
as key=value
so I need to split on comma ,
and then load cossn
as key and 0
its value.
String payload = "cossn=0, abc=hello/=world, Agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
HashMap<String, String> holder = new HashMap();
String[] keyVals = payload.split(", ");
for(String keyVal:keyVals) {
String[] parts = keyVal.split("=",2);
holder.put(parts[0], parts[1]);
}
I am getting java.lang.ArrayIndexOutOfBoundsException
at this line holder.put(parts[0], parts[1]);
and it is happening bcoz of this String Agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
since it has an extra comma in the value KHTML, like Gecko
.
How can I fix this? In general below should be my keys and value after loading it in a map.
Key Value
cossn 0
abc hello/=world
Agent Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36