How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?
In my case I didn't want any dependancies. Using Java 8 you can get JSON as a string this simple:
Map map = new HashMap<>();
map.put("key", "value");
map.put("key2", "value2");
String json = "{"+map.entrySet().stream()
.map(e -> "\""+ e.getKey() + "\"" + ":\"" + String.valueOf(e.getValue()) + "\"")
.collect(Collectors.joining(", "))+"}";