Convert Map to Json using Gson
Gson (also known as Google Gson) is an open source Java library released by Google. Its main purpose is to serialize Java objects into JSON strings or deserialize JSON strings into Java objects.
Gson's POM dependency copy code is as follows:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
The code copy code is as follows:
/**
* Convert Map to Json
*
* @parammap
* @returnString
*/
public static <T> String mapToJson(Map<String, T> map) {
Gson gson = new Gson();
String jsonStr = gson.toJson(map);
return jsonStr;
}
PS: Regarding json operations, here are some more practical json online tools recommended for your reference:
Online JSON code inspection, inspection, beautification, and formatting tools:
http://tools.VeVB.COm/code/json
JSON online formatting tool:
http://tools.VeVB.COm/code/jsonformat
Online XML/JSON conversion tool:
http://tools.VeVB.COm/code/xmljson
JSON code online formatting/beautification/compression/editing/conversion tool:
http://tools.VeVB.COm/code/jsoncodeformat
Online json compression/escaping tool:
http://tools.VeVB.COm/code/json_yasuo_trans
C language style/HTML/CSS/json code formatting and beautification tools:
http://tools.VeVB.COm/code/ccode_html_css_json