Home > Java > javaTutorial > How to Convert a Java HashMap to a JSON String?

How to Convert a Java HashMap to a JSON String?

Susan Sarandon
Release: 2024-12-03 19:29:13
Original
779 people have browsed it

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);
Copy after login

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();
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template