在 JQuery、javascript 中解析 JSON 响应时出错

发布于 2024-12-10 17:48:18 字数 2548 浏览 0 评论 0原文

我收到以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许仙没带伞 2024-12-17 17:48:18

解析没问题。

问题是访问由非字母数字字符组成的键..如空格、破折号等。

这些属性必须使用 [] 表示法处理,如

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

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 this

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

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

无妨# 2024-12-17 17:48:18

您不能执行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 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

要走就滚别墨迹 2024-12-17 17:48:18
$.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.
});
$.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.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文