如何使用jquery删除json中的空值

时间:2013-11-30 17:22:29

标签: jquery json

Iam从本网站提取数据http://www.hktdc.com/sourcing/manufacturers / certificates / en / 1X07XOT7 / Jintan-Chao-Chuang-Battery-Co-Ltd.htm   这是我的编码。我在firebug(firefox)控制台应用程序中工作。 无法删除该空键和值。只是对这个解决方案提出一些想法

var allCertificate={};
allCertificate["CertificateDetails"]={};
allCertificate["CertificateDetails"]["Intertek"]=[];
var c=0;

jQuery("table.print_mainBodyWidth tbody tr:nth-child(2) td table tbody tr:nth-child(2n)").each(function(){

allCertificate["CertificateDetails"]["Intertek"][c]={};
allCertificate["CertificateDetails"]["Intertek"][c]["Name"]=jQuery(this).find("td.background_white_padding_middle").slice(0,1).text();
allCertificate["CertificateDetails"]["Intertek"][c]["Nature"]=jQuery(this).find("td.background_white_padding_middle").slice(1,2).text();

c++
});

alert(JSON.stringify(allCertificate));

这是我在json在线解析器中的输出。

   {  
           "CertificateDetails":{
            "Intertek":[
                {
                    "Name":" ISO9001:2008 ",
                    "Nature":"Management System Certification"
                },
                {
                    "Name":"",
                    "Nature":""
                },
                {
                    "Name":" ISO14001:2004 ",
                    "Nature":"Management System Certification"
                },
                {
                    "Name":"",
                    "Nature":""
                },
                {
                    "Name":" UL ",
                    "Nature":"Product Certification"
                },
                {
                    "Name":"",
                    "Nature":""
                },
                {
                    "Name":"",
                    "Nature":""
                }
            ]
        }

    }


I want the output like this without null values:
{
    "CertificateDetails":{
        "Intertek":[
            {
                "Name":" ISO9001:2008 ",
                "Nature":"Management System Certification"
            },

            {
                "Name":" ISO14001:2004 ",
                "Nature":"Management System Certification"
            },

            {
                "Name":" UL ",
                "Nature":"Product Certification"
            },

        ]
    }

}

请建议我并帮助。

1 个答案:

答案 0 :(得分:0)

只需检查循环中的长度:

var allCertificate = {};
allCertificate["CertificateDetails"] = {};
allCertificate["CertificateDetails"]["Intertek"] = [];
var c=0;

jQuery("table.print_mainBodyWidth tbody tr:nth-child(2) td table tbody tr:nth-child(2n)").each(function(){

    var name   = jQuery(this).find("td.background_white_padding_middle").slice(0,1).text(),
        nature = jQuery(this).find("td.background_white_padding_middle").slice(1,2).text();

    if ( $.trim(name).length && $.trim(nature).length ) {
        allCertificate["CertificateDetails"]["Intertek"][c] = {};
        allCertificate["CertificateDetails"]["Intertek"][c]["Name"] = name;
        allCertificate["CertificateDetails"]["Intertek"][c]["Nature"] = nature;
        c++
    }
});

alert(JSON.stringify(allCertificate));