Java Study Notes 3-json output

Source: Internet
Author: User

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 ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.