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"); }
<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> <bean class="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定义的时候记得要构件一个空函数的构造函数,不然会报一下错误!
参考文档:
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
Spring 接收转换 jquery ajax json数组字符串,布布扣,bubuko.com
Spring 接收转换 jquery ajax json数组字符串
原文地址:http://blog.csdn.net/shuzui1985/article/details/38543969