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