How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?
If you don't really need HashMap then you can do something like that:
String jsonString = new JSONObject() {{ put("firstName", user.firstName); put("lastName", user.lastName); }}.toString();
Output:
{ "firstName": "John", "lastName": "Doe" }