Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting HashMap<String, object> in jsonobject

Tags:

java

json

hashmap

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

like image 341
Jonathan Avatar asked Dec 11 '22 13:12

Jonathan


1 Answers

Use JSONObject constructor. DON"T CREATE YOUR OWN since you might miss some cases such when the value is an array.

JSONObject jsonObject = new JSONObject(hashMap);

This is actually a complete solution since it covers for corner cases such as where the value is an array. Thus, it will make it as JSONArray for you.

like image 181
Multithreader Avatar answered Dec 14 '22 03:12

Multithreader