The map object is converted into a JSON-formatted string string

Source: Internet
Author: User


1 //string,put to map converted to JSON-formatted string string from the action processing page2@RequestMapping ("/seleteoaorder")3 @ResponseBody4 PublicObject seleteoaorder (String param) {//param = "xiaoming";5System.out.println ("+++++++++++++++++param:" +param);6 if(param!=NULL){7Requestentity request =Newrequestentity ();8Request.setusername ("0755pss001");9Request.setpwd ("PSS");Tenmap<string,string> map =NewHashmap<string, string>(); OneMap.put ("Applicant", param); ASystem.out.println ("++++++++++++++++++ to JSON before the map object:" +map.tostring ()); -Objectmapper JSON =NewObjectmapper (); -String params =NULL; the Try { - //convert a Map object to a JSON-formatted string string -params =json.writevalueasstring (map); -System.out.println ("++++++++++++++++++ converted to JSON format string:" +params); +}Catch(jsongenerationexception e) { - //TODO auto-generated Catch block + e.printstacktrace (); A}Catch(jsonmappingexception e) { at //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } in request.setparams (params); -Responseentity response =Buyorderservice.seleteoaorder (request); to returnResponse.getresult (); + } - return NULL; the}
Results of the spool print:
Map object before transcoding to JSON: {applicant: Xiaoming}
Converted JSON format string: {"Applicant": "Xiao Ming"};
The new map object can be written as map<string,integer> map = new hashmap<string, integer> ();
Here param is an int, equal to 2,
Map object before transcoding to JSON: {applicant:2}
Converted JSON format string: {"Applicant": 2}; There are no double quotes here param.
The main use of objectmapper (
Import org.codehaus.jackson.JsonGenerationException;
Import org.codehaus.jackson.map.JsonMappingException;
Import Org.codehaus.jackson.map.ObjectMapper;)
Writes a JSON-formatted string to the Map object.

The map object is converted into a JSON-formatted string string

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.