Как удалить нулевые значения в JSON с помощью JQuery

0

Iam извлечение данных с этого сайта http://www.hktdc.com/sourcing/manufacturers/certificates/en/1X07XOT7/Jintan-Chao-Chuang-Battery-Co-Ltd.htm это мое кодирование. iam работает в консольном приложении 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
Лучший ответ

Просто проверьте длину в цикле:

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));

Ещё вопросы

Сообщество Overcoder
Наверх
Меню