2010-08-31 63 views
0

在Windows 7使用1.4.1针对Android的我有一个Web服务是由钛的应用程序访问,服务返回JSON这样的:为什么Appcelerator Titanium Mobile不会解析这个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); 

这将显示一个没有任何内容的警报对话框。钛HTTPClient呼叫工作正常,因为我可以

Ti.debug(this.responseText) 

没有问题。

JSON验证OK,例如,在jsonlint.com。

回答

2

的JSON看起来不错,并解析罚款...但行:

alert(contacts.length); 

,导致你相信它是不工作的唯一部分?由于无法获取对象的长度(VFPData),因此无论是否进行有效解析,都会得到undefined/null。更好的测试是:

alert(contacts.VFPData.rows.length); 

...因为你知道行是一个数组。或者:

alert(contacts); 

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