• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Jquery 遍历Json的实现代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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 如何遍历jsonc对象?其实方法很简单。 

比如有如下json对象: 

var obj ={”name”:”冯娟”,”password”:”123456″,”department”:”技术部”,”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);
    //数组与对象为object类型,其他分别是string与number类型
    //此时的k代表a、b、c、d,v代表对应的值
    if (typeof(v) == "object") {
        //获取数组与对象后,再去遍历这个数组与对象
        $.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]);
        }
    }
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
JS遍历Json字符串发布时间:1970-01-01
下一篇:
JSON数据遍历之for-in发布时间:1970-01-01
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap