1

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);
        }
    });
}
3
  • 2
    Keys with spaces are no problem for the parser. JSON.parse and jQuery's parser will just work fine. See jsfiddle.net/4jqQw ... so the question is what is your actual problem? Commented Oct 19, 2011 at 10:40
  • I think you can use Employees[0]["Employee"]["Employee ID"] instead of Employees[0].Employee.Employee ID(which is not correct) , but I'm not 100% sure Commented Oct 19, 2011 at 10:44
  • I put your JSON into jsonlint.com and it comes up as valid JSON, so a parser shouldn't be breaking on this. What parser are you using? Would you please paste code from your parsing attempt as well as the error you are getting? Commented Oct 19, 2011 at 10:46

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0
$.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.
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.