Using Gson to convert a map to JSON
Gson (also known as Google Gson) is an open-source Java library published by Google, which uses either a serialized Java object as a JSON string or a drag-formatted JSON string as a Java object.
Gson Pom-dependent
Copy Code code as follows:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
Code
Copy Code code as follows:
/**
* Convert map to JSON
*
* @param map
* @return String
*/
public static <T> String Maptojson (map<string, t> Map) {
Gson Gson = new Gson ();
String jsonstr = Gson.tojson (map);
return jsonstr;
}
PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
Online JSON compression/escape tool:
Http://tools.jb51.net/code/json_yasuo_trans
C language Style/html/css/json code formatting Landscaping tool:
Http://tools.jb51.net/code/ccode_html_css_json