Spring 接收轉換 jquery ajax json數組字串

來源:互聯網
上載者:User

標籤:spring   json   

1、ajax發送json字串

</pre><p>組建對象</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 = "柯樂義";student.age = "25";student.location = "廣州";var student2 = new Object();student2.name = "柯範德薩";student2.age = "45";student2.location = "FDA";myList.push(student);myList.push(student2);
ajax發送,注意datatype ,type 等的正確賦值,使用jsonstringify對對象進行模型轉換,轉換後的字串可以在http://www.bejson.com/ 進行線上json格式檢查正確與否。

            $.ajax({ //請求登入處理頁                        url:url, //登入處理頁                        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' : 'application/json'   },                        success:function (strValue) { //登入成功後返回的資料                            //根據傳回值進行狀態顯示                            //alert(strValue);                            if (strValue.successful == "true") {                                alert("發送成功@@");                            }                            else {                                alert("發送失敗@@");                            }                        },                        error:function () {                            alert("請檢查是否有參數錯誤@@");                        }                    });


2、spring進行配置


配置requestbody進行json字串的數群組轉換

    @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");}

注意在springmvc-servlet.xml進行如下配置:json轉化器配置

<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轉換器 --><ref bean="mappingxmlHttpMessageConverter" /><!-- xml轉換器 --><ref bean="stringHttpMessageConverter" /><!-- xml轉換器 --></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
類SnapInfo定義的時候記得要構件一個空函數的建構函式,不然會報一下錯誤!

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object



參考文檔:
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.