先給大家說(shuō)下我的需求:進(jìn)行ajax請(qǐng)求后,后臺(tái)傳遞回來(lái)以下json數(shù)據(jù)。
具體實(shí)現(xiàn)代碼如下所示:
JavaScript代碼
{
"data":[
{"id":"1","name":"選擇A","value":"A"},
{"id":"2","name":"選擇B","value":"B"},
{"id":"3","name":"選擇C","value":"C"}
]
}
對(duì)上面的json數(shù)據(jù)類在jquery 的success 函數(shù)中解析
JavaScript代碼
$.ajax({
type: "POST",
url: "xxx.do",
dataType : "json", // 指定返回類型
data: {xxx:"yyy"}, // 傳遞到后臺(tái)的參數(shù)
success: function(data)
{
$.each(data, function(index,values){ // 解析出data對(duì)應(yīng)的Object數(shù)組
$.each(values,function(index2,value){ // 遍歷Object數(shù)組 ,每個(gè)對(duì)象的值存放在value ,index2表示為第幾個(gè)對(duì)象
// 根據(jù)自己的邏輯進(jìn)行數(shù)據(jù)的處理
alert(value.name + " " + value.value);
// TODO: 邏輯
});
});
},
error : function()
{
alert("系統(tǒng)出現(xiàn)問(wèn)題");
}
});
繼續(xù)來(lái)個(gè)例子
本章節(jié)分享一段代碼實(shí)例,是jQuery封裝的ajax對(duì)JSON數(shù)據(jù)的請(qǐng)求處理。
代碼比較簡(jiǎn)單,對(duì)于有經(jīng)驗(yàn)的人員來(lái)說(shuō)可以掠過(guò),初學(xué)者可以做一下參考之用。
代碼如下:
!DOCTYPE html>
html>
head>
meta charset=" utf-8">
title>腳本之家/title>
script src="http://libs.baidu.com/jquery/1.9.0/jquery.js">/script>
script>
$(document).ready(function(){
$("#bt").click(function(){
$.ajax({
type: "get",
dataType: "json",
url: "demo/jQuery/ajax/txt/json.txt",
success: function(msg){
var data = msg
str="";
$.each(data,function(index, n){
str=str+data[index].webName+","+data[index].url+","+data[index].age+"br/>";
});
$("#show").html(str);
}
});
})
})
/script>
/head>
body>
div id="show">/div>
input type="button" id="bt" value="查看效果"/>
/body>
/html>
以上所述是小編給大家介紹的jQuery ajax json 數(shù)據(jù)的遍歷代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- JQuery遍歷json數(shù)組的3種方法
- jquery對(duì)Json的各種遍歷方法總結(jié)(必看篇)
- 遍歷json獲得數(shù)據(jù)的幾種方法小結(jié)
- jQuery的ajax和遍歷數(shù)組json實(shí)例代碼
- jquery動(dòng)態(tài)遍歷Json對(duì)象的屬性和值的方法
- jQuery篩選數(shù)組之grep、each、inArray、map的用法及遍歷json對(duì)象
- jQuery遍歷json的方法(推薦)
- jQuery通過(guò)ajax請(qǐng)求php遍歷json數(shù)組到table中的代碼(推薦)
- jquery遍歷json對(duì)象集合詳解
- jQuery使用$.each遍歷json數(shù)組的簡(jiǎn)單實(shí)現(xiàn)方法
- jQuery遍歷json的方法分析
- jquery使用each方法遍歷json格式數(shù)據(jù)實(shí)例
- jQuery遍歷json中多個(gè)map的方法
- jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對(duì)象
- 通過(guò)Jquery遍歷Json的兩種數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)代碼
- jQuery 遍歷json數(shù)組的實(shí)現(xiàn)代碼
- Javascript Jquery 遍歷Json的實(shí)現(xiàn)代碼
- Jquery遍歷Json數(shù)據(jù)的方法