【问题标题】:Why won't Appcelerator Titanium Mobile parse this JSON?为什么 Appcelerator Titanium Mobile 不解析这个 JSON?
【发布时间】:2010-08-31 11:52:38
【问题描述】:

在 Windows 7 下针对 Android 使用 1.4.1。我有一个 Web 服务正在从 Titanium 应用程序访问,该服务返回 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": "Mark@ovl.co.uk",
                "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 中。

【问题讨论】:

    标签: ajax titanium appcelerator appcelerator-mobile


    【解决方案1】:

    JSON 看起来不错,解析也不错……但就是这样:

    alert(contacts.length);
    

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

    alert(contacts.VFPData.rows.length);
    

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

    alert(contacts);
    

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

    【讨论】:

      猜你喜欢
      • 2015-11-03
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2012-06-20
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      相关资源
      最近更新 更多