在 JQuery、javascript 中解析 JSON 响应时出错
我收到以下 JSON 响应,但某些索引包含“员工 ID”等空格。这就是为什么我无法解析它。谁能建议用 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]"
}
}
]
}
我的代码 -
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);
}
});
}
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);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解析没问题。
问题是访问由非字母数字字符组成的键..如空格、破折号等。
这些属性必须使用
[]
表示法处理,如http://jsfiddle.net/h9sbn/
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 thisDemo at http://jsfiddle.net/h9sbn/
您不能执行
fnalObj.Employee.Ext - 1
。正确的方法是fnalObj.Employee['Ext-1']
。这里是 jsFiddle http://jsfiddle.net/naryad/VNXa5/1/使用时
fnalObj.Employee.Ext - 1
,它被解析为undefined - 1
,进而返回您NaN
相同适用于
fnalObj.Employee.Mobile - 1
You cannot do
fnalObj.Employee.Ext - 1
. The correct way to do it would befnalObj.Employee['Ext-1']
. Here is the jsFiddle http://jsfiddle.net/naryad/VNXa5/1/When using
fnalObj.Employee.Ext - 1
, it gets resolved toundefined - 1
which in turn returns youNaN
Same applies to
fnalObj.Employee.Mobile - 1