Turn a map into a json example using gson

  • 2020-04-01 03:18:10
  • OfStack

Convert Map to Json using Gson

Gson (also known as Google Gson) is an open source Java library released by Google. It is mainly used to serialize Java objects into JSON strings, or to deserialize JSON strings into Java objects.

Gson's POM dependency


<dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>2.2.4</version>
</dependency>

code


/**
 * will Map into 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 again for you to recommend a few more practical json online tools for your reference:

Online JSON code verification, verification, beautification, formatting tools:
(link: http://tools.jb51.net/code/json)

JSON online formatting tool:
(link: http://tools.jb51.net/code/jsonformat)

Online XML/JSON interconversion tool:
(link: http://tools.jb51.net/code/xmljson)

Json code online formatting/beautification/compression/editing/conversion tools:
(link: http://tools.jb51.net/code/jsoncodeformat)

Online json compression/escape tool:

(link: http://tools.jb51.net/code/json_yasuo_trans)

C language style /HTML/CSS/json code format beautification tool:
(link: http://tools.jb51.net/code/ccode_html_css_json)


Related articles: