jonney jonney - 6 months ago 13
Java Question

Putting HashMap<String, object> in jsonobject

i building a json object that consists of nameValue pairs defined in a Hashmap

the issue i am having is when i invoke:

jsonObject.put(hashmap);


It adds the nameValue pairs like this:

name=value
instead of
name:value


Any thoughts?

Thanks

Answer

Iterate through the HashMap and put to the jsonObject:

Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    jsonObject.put(pairs.getKey(), pairs.getValue() );
}