为什么 Appcelerator Titanium Mobile 无法解析此 JSON?

发布于 2024-09-16 20:22:27 字数 1780 浏览 5 评论 0原文

在 Windows 7 下针对 Android 使用 1.4.1。我有一个从 Titanium 应用程序访问的 Web 服务,该服务返回如下 JSON:

{
    "VFPData": {
        "rows": [
            {
                "address1": "Orion House",
                "address2": "Orion Way",
                "address3": "Kettering",
                "address4": "Northants",
                "comp_name": "Orion Vehicles Leasing",
                "contid": 1,
                "email": "",
                "email2": "",
                "fax": "",
                "firstname": "David John",
                "lastname": "Sear",
                "mobile": "",
                "phone1": "",
                "phone2": "",
                "postcode": "NN15 6PE"
            },
            {
                "address1": "Unit 20 Acton Business Park",
                "address2": "Acton Lane",
                "address3": "London",
                "address4": "",
                "comp_name": "Orion Vehicles Limited",
                "contid": 2,
                "email": "[email protected]",
                "email2": "",
                "fax": "",
                "firstname": "Mark",
                "lastname": "Johnson",
                "mobile": "0888 566 67879",
                "phone1": "0208 209 1359",
                "phone2": "",
                "postcode": "NW10 7NH"
            }
        ]
    }
}

但是,eval 或 JSON.parse 的组合不会返回有效结果 - 例如:

var contacts = JSON.parse(this.responseText);
alert(contacts.length);

这将显示一个没有任何内容的警告对话框。 Titanium HTTPClient 调用工作正常,

Ti.debug(this.responseText) 

没有任何问题。

该 JSON 也验证正常,例如在 jsonlint.com 中。

Using 1.4.1 against Android under Windows 7. I have a web service being access from the Titanium application, the service returns JSON like this:

{
    "VFPData": {
        "rows": [
            {
                "address1": "Orion House",
                "address2": "Orion Way",
                "address3": "Kettering",
                "address4": "Northants",
                "comp_name": "Orion Vehicles Leasing",
                "contid": 1,
                "email": "",
                "email2": "",
                "fax": "",
                "firstname": "David John",
                "lastname": "Sear",
                "mobile": "",
                "phone1": "",
                "phone2": "",
                "postcode": "NN15 6PE"
            },
            {
                "address1": "Unit 20 Acton Business Park",
                "address2": "Acton Lane",
                "address3": "London",
                "address4": "",
                "comp_name": "Orion Vehicles Limited",
                "contid": 2,
                "email": "[email protected]",
                "email2": "",
                "fax": "",
                "firstname": "Mark",
                "lastname": "Johnson",
                "mobile": "0888 566 67879",
                "phone1": "0208 209 1359",
                "phone2": "",
                "postcode": "NW10 7NH"
            }
        ]
    }
}

However no combination of eval or JSON.parse will return a valid result - for example:

var contacts = JSON.parse(this.responseText);
alert(contacts.length);

That will show an alert dialog with nothing in it. The Titanium HTTPClient calls are working fine as I can

Ti.debug(this.responseText) 

with no problem.

That JSON validates OK as well, in jsonlint.com for example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

春庭雪 2024-09-23 20:22:27

JSON 看起来很好,解析也很好...但是这行是:

alert(contacts.length);

唯一让你相信它不起作用的部分吗?因为您无法获取对象(VFPData)的长度...无论是否发生有效的解析,您都会得到未定义/空值。更好的测试是:

alert(contacts.VFPData.rows.length);

...因为您知道 rows 是一个数组。或者:

alert(contacts);

应该报告它是一个对象(如果已解析)或 null/未定义,否则。

The JSON looks fine, and parses fine... but is the line:

alert(contacts.length);

the only part that leads you to believe it isn't working? Because you can't get the length of an object (VFPData)... you'll get undefined/null whether valid parsing happened or not. A better test is:

alert(contacts.VFPData.rows.length);

... since you know rows is an array. Or:

alert(contacts);

Which should report it's an object (if parsed) or null/undefined otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文