ashu
ashu

Reputation: 13

Error in parsing the JSON response in JQuery, javascript

I am getting following as the JSON response but some indexes are having spaces like 'Employee Id'. That's why I am unable to parse it. Can anyone suggest the way to parse it in JavaScript?

{
    "Employees": [
        {
            "Employee": {
                "Employee ID": "777",
                "Short Name": "abc",
                "First name": null,
                "Middle name": null,
                "Last name": null,
                "Designation": "Senior Engineer",
                "Ext-1": null,
                "Ext-2": null,
                "Mobile-1": null,
                "Mobile-2": null,
                "Email": "[email protected]"
            }
        },
        {
            "Employee": {
                "Employee ID": "888",
                "Short Name": "xyz",
                "First name": null,
                "Middle name": null,
                "Last name": null,
                "Designation": "Test Lead",
                "Ext-1": null,
                "Ext-2": null,
                "Mobile-1": null,
                "Mobile-2": null,
                "Email": "[email protected]"
            }
        }
    ]
}

My code -

 function GetContacts() {
    $.ajax({
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        url: "http://. . . . . .",

        dataType: "json",
        success: function(data) {

            //alert(data.getString("Employee ID"));
            $.each(data, function(i, contactList) {

                alert('First Loop' + i);
                alert('First Loop' + contactList[0]);

                $.each(contactList, function(j, Contact) {
                    //alert('Second Loop'+Contact);
                    var fnalObj = Contact;
                    //alert(fnalObj);
                    //alert(fnalObj.["Employee"]["Employee ID"]);
                    //alert(Employees[j]["Employee"]["Email"]);
                    //alert(Employees[0]["Employee"]["Employee ID"]);
                    alert(fnalObj.Employee.Email);
                    alert(fnalObj.Employee.Designation);
                    alert(fnalObj.Employee.Ext - 1);
                    alert(fnalObj.Employee.Mobile - 1);
                });
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
            alert(textStatus);
        }
    });
}

Upvotes: 1

Views: 1047

Answers (3)

Mike Thomsen
Mike Thomsen

Reputation: 37506

$.each(contactList, function(j, Contact) {
    //alert('Second Loop'+Contact);
    var fnalObj = Contact;
    $.each(finalObj, function (key, value) {
        var newKey = key.replace(/[\s]\-/g, '_');
        delete finalObj[key];
        finalObj[newKey] = value;
    });
    //revised alerts here.
});

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

Parsing is fine.

The problem is accessing the keys which are made of non-alhpanumeric characters.. like spaces, dashes etc..

These properties must be handled with the [] notation like this

alert(fnalObj.Employee['Employee ID']);
alert(fnalObj.Employee['Ext-1']);

Demo at http://jsfiddle.net/h9sbn/

Upvotes: 3

Narendra Yadala
Narendra Yadala

Reputation: 9664

You cannot do fnalObj.Employee.Ext - 1. The correct way to do it would be fnalObj.Employee['Ext-1']. Here is the jsFiddle http://jsfiddle.net/naryad/VNXa5/1/

When using fnalObj.Employee.Ext - 1, it gets resolved to undefined - 1 which in turn returns you NaN

Same applies to fnalObj.Employee.Mobile - 1

Upvotes: 2

Related Questions