Home >Java >javaTutorial >How to Convert a Java HashMap to a JSON String?
Converting HashMap to JSON Object in Java
Question:
How can I convert a HashMap to a JSON object in Java, and then further convert it to a JSON string?
Answer:
To convert a HashMap to a JSON object, you can use the following code:
JSONObject jsonObject = new JSONObject(hashMap);
This will create a JSON object from the provided HashMap.
To convert the JSON object to a JSON string, you can use the following code:
String jsonString = jsonObject.toString();
This will return a string representation of the JSON object.
Additional Functionality:
The JSON-java library provides various other functions for working with JSON data. You can find more information in the library's documentation:
http://stleary.github.io/JSON-java/index.html
The above is the detailed content of How to Convert a Java HashMap to a JSON String?. For more information, please follow other related articles on the PHP Chinese website!