jQuery 遍歷Json的實現(xiàn)代碼

2023-04-17 15:34 更新

在前端開發(fā)中,很多時候我們會使用jQuery提高開發(fā)效率,jQuery也有遍歷JSON的方法,代碼如下:

/* 
author: liulf 
function: 
var __db_cpcj=vCpcj; 
var __db_cp=vCp; 
var vCp={"cppage":[ 
{"cpdh":"A01000101","page":"1","ca":"1309-36-0","mc":"硫鐵礦","ename":"Pyrite","bm":"黃鐵礦"}, 
{"cpdh":"A01000201","page":"1","ca":"1309-36-0","mc":"硫精砂","ename":"Pyrite concentrate;Sulfur concentrate","bm":""}, 
{"cpdh":"A01000202","page":"1","ca":"1309-36-0","mc":"硫精礦粉","ename": 
"Powdered pyrite concentrate;Pyrites concentrate,powder;Sulfur concentrate,powder","bm":"硫精礦"},
{"cpdh":"A02000101","page":"1","ca":"","mc":"磷礦石","ename":"Phosphorus ore","bm":""}, 
{"cpdh":"A02000111","page":"2","ca":"","mc":"磷礦砂","ename":"Phosphorite grit","bm":""}, 
{"cpdh":"A02000201","page":"2","ca":"","mc":"磷礦粉","ename":"Phosphate rock powder","bm":""},
{"cpdh":"A03000101","page":"2","ca":"12447-04-0","mc":"硼礦石","ename":"Ascharite;Boric ore","bm":"硼鎂礦"}, 
{"cpdh":"A04000101","page":"2","ca":"","mc":"鉀長石","ename":"Potash feldspar;Potassium feldspars","bm":""}, 
{"cpdh":"A04000201","page":"2","ca":"12003-63-3","mc":"長石粉","ename":"Feldspar powder","bm":""}, 
{"cpdh":"A04000301","page":"2","ca":"","mc":"光鹵石","ename":"Carnallite","bm":""} 
]} 
*/
function ObjSearch() {
    this.kw = '';
    this.searchCpcj = function() {
        $(vCpcj).each(function() {});
    }
    this.searchCp = function() {
        // var json = eval(vCp.cppage) 
        // for(var i=0; i<json.length; i++) 
        // { 
        // alert(json[i].cpdh+" " + json[i].page) 
        // break; 
        // } 
        $.each(vCp.cppage, function(index, content) {
            document.write('<div>' + content.cpdh + '</div><br />');
        });
    }
}

js 如何遍歷json對象?其實方法很簡單。 

比如有如下json對象: 


var obj ={”name”:”馮娟”,”password”:”123456″,”department”:”技術(shù)部”,”sex”:”女”,”old”:30}; 

遍歷方法: 

代碼如下:
for (var p in obj) {
    str = str + obj[p] + ',';
    return str;
}

綜合實例:

var array = {
    "a": "abc",
    "b": [1, 2, 3, 4, 5, 6],
    "c": 3,
    "d": {
        "name": "james",
        "age": 28
    },
    "e": null,
    "f": true
};
var arrayObj = {
    "a": {
        "name": "kobe",
        "age": 34
    },
    "b": {
        "name": "lofo",
        "age": 28
    }
};
//遍歷array方式1
for (var x in array) {
    if (typeof array[x] == 'object' && array[x] != null) {
        for (var y in array[x]) {
            alert("key = " + y + " value = " + array[x][y]);
        }
    } else {
        alert("key = " + x + " value = " + array[x]); // 非array object
    }
}
//遍歷array方式2
$.each(array, function(k, v) {
    var a = typeof(v);
    //數(shù)組與對象為object類型,其他分別是string與number類型
    //此時的k代表a、b、c、d,v代表對應的值
    if (typeof(v) == "object") {
        //獲取數(shù)組與對象后,再去遍歷這個數(shù)組與對象
        $.each(v, function(k1, v1) {
            window.alert("key = " + k1 + " value=" + v1);
        });
    } else {
        window.alert("key = " + k + " value=" + v);
    }
});
//遍歷arrayObj方式1
for (var x in arrayObj) {
    for (var key in arrayObj[x]) {
        window.alert("key=" + key + " value=" + arrayObj[x][key]);
    }
}
//遍歷arrayObj方式2
$.each(arrayObj, function(key, value) {
    $.each(value, function(k, v) {
        window.alert("key=" + k + " value=" + v);
    });
});
//遍歷arrayObj方式3
$.each(arrayObj, function(key, value) {
    window.alert("key=" + key + " valueName=" + value.name + " valueAge=" + value.age);
});
var UserList = [
    { "UserID": 11, "Name": { "FirstName": "Truly", "LastName": "Zhu" }, "Email": "" },
    { "UserID": 12, "Name": { "FirstName": "Jeffrey", "LastName": "Richter" }, "Email": "" },
    { "UserID": 13, "Name": { "FirstName": "Scott", "LastName": "Gu" }, "Email": "" }
]
$.each(UserList, function(k, v) {
    $.each(v, function(k1, v1) {
        if (typeof(v1) == "object") {
            $.each(v1, function(k2, v2) {
                window.alert("key=" + k2 + " value=" + v2);
            });
        } else {
            window.alert("key=" + k1 + " value=" + v1);
        }
    });
});
var userArray = [
    { "UserID": 11, "Name": { "FirstName": "Truly", "LastName": "Zhu" }, "Email": "" },
    { "UserID": 12, "Name": { "FirstName": "Jeffrey", "LastName": "Richter" }, "Email": "" },
    { "UserID": 13, "Name": { "FirstName": "Scott", "LastName": "Gu" }, "Email": "" }
];
for (var i in userArray) {
    for (var k in userArray[i]) {
        if (typeof userArray[i][k] == "object" && userArray[i][k] != null) {
            for (var j in userArray[i][k]) {
                window.alert("key=" + j + "--value=" + userArray[i][k][j]);
            }
        } else {
            window.alert("key=" + k + "--value=" + userArray[i][k]);
        }
    }
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號