jquery 如何遍歷json


 var obj = {"status":1,"bkmsg":"\u6210\u529f","bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"]}{"status":1,"bkmsg":"\u6210\u529f","bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"]},{"status":1,"bkmsg":"\u6210\u529f","bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u4ec0\u4e48\u4e5f\u6ca1\u6709"]}

 

//===================================================================

ajax請求:

$.ajax({ 
        url: '/path/to/file', 
        type: 'GET', 
        dataType: 'json', 
        data: {param1: 'value1'}, 
        success: function (obj){  
            //遍歷obj 
        } 
    })  

返回的內容在success的函數里面,所有的遍歷操作都是在這里面操作的: 

for循環:

var obj = { 
        "status":1, 
        "bkmsg":"\u6210\u529f", 
        "bkdata":["\u5415\u5c1a\u5fd7","1387580400","\u6dfb\u52a0\u8bb0\u5f55"] 
    } 
   // console.log(obj.length); 
    if (obj.status == 1) { 
        for (var i = 0; i < obj.bkdata.length; i++) { 
            console.log(obj.bkdata[i]); 
        }; 
    }else{ 
        alert("數據有誤~"); 
    };  

for in 循環:

//for in循環 
    for(x in obj.bkdata){ 
        //x表示是下標,來指定變量,指定的變量可以是數組元素,也可以是對象的屬性。 
        console.log(obj.bkdata[x]); 
    }  

//元素 each方法

 if (obj.status == 1) { 
        $(obj.bkdata).each(function(index,item){ 
            //index指下標 
            //item指代對應元素內容 
            //this指代每一個元素對象 
            //console.log(obj.bkdata[index]); 
            console.log(item); 
            //console.log($(this)); 
        }); 
    }else{ 
        alert("數據有誤~"); 
    };  

//jquery each方法

$.each( obj.bkdata, function(index,item){ 
        console.log(item); 
    });  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM