標籤: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