为什么 Appcelerator Titanium Mobile 无法解析此 JSON?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSON 看起来很好,解析也很好...但是这行是:
唯一让你相信它不起作用的部分吗?因为您无法获取对象(VFPData)的长度...无论是否发生有效的解析,您都会得到未定义/空值。更好的测试是:
...因为您知道 rows 是一个数组。或者:
应该报告它是一个对象(如果已解析)或 null/未定义,否则。
The JSON looks fine, and parses fine... but is the line:
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:
... since you know rows is an array. Or:
Which should report it's an object (if parsed) or null/undefined otherwise.