Spring receives convert jquery Ajax JSON array string

Source: Internet
Author: User

1. Ajax sends JSON string

</pre><p> Build Object </p><p></p><pre code_snippet_id= "449843" Snippet_file_name= "Blog_ 20140813_2_7927326 "name=" code "class=" javascript ">var student = new Object (); student.name =" Collayi "; student.age =" 25 " ; student.location = "Guangzhou"; var student2 = new Object (); student2.name = "Covan de Sa"; student2.age = ""; student2.location = "FDA"; Mylist.push (student); Mylist.push (Student2);
Ajax is sent, note the correct assignment of datatype, type, etc., using jsonstringify to model the object, the converted string can be checked correctly in the online JSON format http://www.bejson.com/.

            $.ajax ({//Request login Processing page url:url,//Login Processing page dataType: "JSON", Data:JSON.stringify (<span style= "font-family:arial, Helvetica, Sans-serif;" >mylist</span><span style= "font-family:arial, Helvetica, Sans-serif;" >), </span>type: "POST", headers: {' Accept ': ' Application/json ', ' content-type ': ' Appli Cation/json '}, Success:function (strvalue) {//data returned after successful login//status based on return value                            Display//alert (strvalue);                            if (strvalue.successful = = "true") {alert ("Send success @@");                            } else {alert ("Send failed @@");                        }}, Error:function () {alert ("Please check for parameter error @@");                }    }); 


2, Spring Configuration


Configuring Requestbody for array conversion of JSON strings

    @RequestMapping (value= "/upload", produces= "Application/json")    @ResponseBody public    String Login ( HttpServletRequest req, HttpServletResponse reponse,    String from,string ID, @RequestBody snapinfo[] aplist) throws unsupportedencodingexception {    String result;        Logger.debug ("Start upload! "+from +", "+id");        Logger.debug (aplist.length);        result = Jsonutil.sendjsonresponse (True, "success");        Reponse.setcontenttype ("text/html; Charset=utf-8 ");        Reponse.setcharacterencoding ("Utf-8");        return new String (Result.getbytes ("Utf-8"), "iso-8859-1");}

Note the following configuration in Springmvc-servlet.xml: JSON converter configuration

<bean name= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "><property name=" Supportedmediatypes "><list><value>text/html;charset=utf-8</value><value>application /json</value></list></property></bean><beanclass= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingjacksonhttpmessageconverter "/><!--JSON Converter-- <ref bean= "Mappingxmlhttpmessageconverter"/><!--XML converter--><ref bean= "Stringhttpmessageconverter"/ ><!--XML Converter--></list></property><!--property name= "Messageconverters" > <list> <bean class= "Org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class= " Org.springframework.http.converter.xml.SourceHttpMessageConverter "/> <bean class=" org.springframework.http .Converter.xml.XmlAwareFormHttpMessageConverter "/> <bean class=" Org.springframework.http.converter.StringHttpMessageConverter "> <property name=" Supportedmediatypes "> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </ List> </property--></bean>

Class Snapinfo
When you define a class snapinfo, remember to construct the constructor of an empty function, or you will report an error!

Jsonmappingexception:no suitable constructor found for type [Simple Type, class]: Can-not-instantiate from JSON object



Reference Documentation:
http://www.bejson.com/
Http://www.w3school.com.cn/jquery/ajax_post.asp
Http://keleyi.com/a/bjac/8p778pqo.htm
Http://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class
http://seaboycs.iteye.com/blog/1997635
http://greatpwx.iteye.com/blog/1974150

Related Article

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.