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

來源:互聯網
上載者:User

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

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


spring mvc json格式數組到後台數組的轉換

springmvc後台ajax調用中字串到jquery中的json對象和數組對象轉換問題

查看jquery文檔,我們知道jquery有很多種Ajax調用方法,下面結合springmvc返回的資料,假設
的是data ='{"label":"1","value":[{"id":1,"pid":null,"name":"娛樂"},{"id":101,"pid":1,"name":"音樂"},{"id":112,"pid":1,"name":"卡通"}],"text":""}',
則在如下的ajax調用和回調中,可以參考範例:
$.get("test.do", function(data){
alert("Data Loaded: " + data);
var jsonObj = eval('(' + data + ')'); //獲得jsonObj對象
alert(jsonObj.label);
alert(jsonObj.value);
var arrObj = eval(jsonObj.value);//然後就可以對arrObj做數組運算了
alert(arrObj );}

結果:
data 擷取到'{"label":"1","value":[{"id":1,"pid":null,"name":"娛樂"},{"id":101,"pid":1,"name":"音樂"},{"id":112,"pid":1,"name":"卡通"}],"text":""}';
json.label擷取到1;
json.value擷取到[{"id":1,"pid":null,"name":"娛樂"},{"id":101,"pid":1,"name":"音樂"},{"id":112,"pid":1,"name":"卡通"}]數組;
arrObj 擷取到":[{"id":1,"pid":null,"name":"娛樂"},{"id":101,"pid":1,"name":"音樂"},{"id":112,&q......餘下全文>>
 
jquery ajax 遍曆從服務端接收的複雜json 數組,幫忙

我也看煩了,看著似乎有點明白,但又不怎麼明白,先把代碼寫出來,再說說你這個json
代碼如下:
$.each(h, function () {//h就是你的json了
var h1 = this;
alert("a:" + h1.a);
$.each(h1.a1, function () {
var h2 = this;
alert("a2:" + h2.a2);
$.each(h2.a4, function () {
alert(this.a5);
});
});
});

現在來說你這個json,首先看的出,這是你直接寫出來的,因為有明顯的錯誤,像["a3":a3,"a7"]這裡,a7沒有值
再說數組,你把花括弧和中括弧搞反了,應該是類擬以下形式:
[{"a1":a1,"a2":a2},{"a1":a3,"a2":a4}]
再說說你這個數組中的變數名,以最外層的為例:數組中的變數名第一個是a,第二個變成了b,這這這這這這怎麼把值取出來呀,難道真是要逐個地寫嗎?

搞大了,寫一段完整的吧:
var h=[{"a":"江蘇省","a1":[{"a2":"無錫市","a3":[{"a4":"無X區"},{"a4":"無XX區"}]},{"a2":"蘇州市","a3":[{"a4":"蘇X區"},{"a4":"蘇XX區"}]}]},{"a":"安徽省","a1":[{"a2":"蕪湖市","a3":[{"a4":"蕪X區"},{"a4":"蕪XX區"}]},{"a2":"滁州市","a3":[{"a4":"滁X區"},{"a4":"滁XX區"}]}]}]
$.each(h, function () {//h就是你的json了
var h1 = this;
alert("a:" + h1.a);
$.each(h1.a1, function () {
var h2 = this;
alert("a2:&quo......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.