During interface development, the json format content is generally returned. In php, The json_encode function is used directly. How can this function be implemented in java? After studying it, gson is used to implement a small example 1. download the Gson jar package. I use pom. add this section to get the xml configuration. maven automatically downloads the dependency package [html] <dependency> <groupId> com. google. code. gson </groupId> <artifactId> gson </artifactId> <version> 2.2.2 </version> </dependency> can also be manually downloaded from google. // A hashmap for converting json hashMap <String, object> map = new HashMap <String, Object> (); map. put ("a", 1); map. put ("B", "bbb"); // instantiate gson Gson gson = new Gson (); // convert map to json String str = gson. toJson (map); // sets the json header and outputs The json content response. setHeader ("Content-type", "application/json"); response. getWriter (). println (str); 3. in this way, if you use more places, you can also use the annotation method to save the need for new Gson () for each call ().