How to convert hashmap to JSON object in Java

mohan picture mohan · Aug 28, 2012 · Viewed 724k times · Source

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

Answer

Ankur picture Ankur · Aug 28, 2012

You can use:

new JSONObject(map);

Caution: This will only work for a Map<String, String>!

Other functions you can get from its documentation
http://stleary.github.io/JSON-java/index.html