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);
}
});
}
JSON.parseand jQuery's parser will just work fine. See jsfiddle.net/4jqQw ... so the question is what is your actual problem?Employees[0]["Employee"]["Employee ID"]instead ofEmployees[0].Employee.Employee ID(which is not correct) , but I'm not 100% sure