{"id":825,"date":"2022-08-30T15:07:48","date_gmt":"2022-08-30T15:07:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-wont-appcelerator-titanium-mobile-parse-this-json-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:48","modified_gmt":"2022-08-30T15:07:48","slug":"why-wont-appcelerator-titanium-mobile-parse-this-json-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-wont-appcelerator-titanium-mobile-parse-this-json-collection-of-common-programming-errors\/","title":{"rendered":"Why won&#39;t Appcelerator Titanium Mobile parse this JSON?-Collection of common programming errors"},"content":{"rendered":"<p>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:<\/p>\n<pre><code>{\n    \"VFPData\": {\n        \"rows\": [\n            {\n                \"address1\": \"Orion House\",\n                \"address2\": \"Orion Way\",\n                \"address3\": \"Kettering\",\n                \"address4\": \"Northants\",\n                \"comp_name\": \"Orion Vehicles Leasing\",\n                \"contid\": 1,\n                \"email\": \"\",\n                \"email2\": \"\",\n                \"fax\": \"\",\n                \"firstname\": \"David John\",\n                \"lastname\": \"Sear\",\n                \"mobile\": \"\",\n                \"phone1\": \"\",\n                \"phone2\": \"\",\n                \"postcode\": \"NN15 6PE\"\n            },\n            {\n                \"address1\": \"Unit 20 Acton Business Park\",\n                \"address2\": \"Acton Lane\",\n                \"address3\": \"London\",\n                \"address4\": \"\",\n                \"comp_name\": \"Orion Vehicles Limited\",\n                \"contid\": 2,\n                \"email\": \"Mark@ovl.co.uk\",\n                \"email2\": \"\",\n                \"fax\": \"\",\n                \"firstname\": \"Mark\",\n                \"lastname\": \"Johnson\",\n                \"mobile\": \"0888 566 67879\",\n                \"phone1\": \"0208 209 1359\",\n                \"phone2\": \"\",\n                \"postcode\": \"NW10 7NH\"\n            }\n        ]\n    }\n}\n<\/code><\/pre>\n<p>However no combination of eval or JSON.parse will return a valid result &#8211; for example:<\/p>\n<pre><code>var contacts = JSON.parse(this.responseText);\nalert(contacts.length);\n<\/code><\/pre>\n<p>That will show an alert dialog with nothing in it. The Titanium HTTPClient calls are working fine as I can<\/p>\n<pre><code>Ti.debug(this.responseText) \n<\/code><\/pre>\n<p>with no problem.<\/p>\n<p>That JSON validates OK as well, in jsonlint.com for example.<\/p>\n<ol>\n<li>\n<p>The JSON looks fine, and parses fine&#8230; but is the line:<\/p>\n<pre><code>alert(contacts.length);\n<\/code><\/pre>\n<p>the only part that leads you to believe it isn&#8217;t working? Because you can&#8217;t get the length of an object (VFPData)&#8230; you&#8217;ll get undefined\/null whether valid parsing happened or not. A better test is:<\/p>\n<pre><code>alert(contacts.VFPData.rows.length);\n<\/code><\/pre>\n<p>&#8230; since you know rows is an array. Or:<\/p>\n<pre><code>alert(contacts);\n<\/code><\/pre>\n<p>Which should report it&#8217;s an object (if parsed) or null\/undefined otherwise.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:47:44. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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: { &#8220;VFPData&#8221;: { &#8220;rows&#8221;: [ { &#8220;address1&#8221;: &#8220;Orion House&#8221;, &#8220;address2&#8221;: &#8220;Orion Way&#8221;, &#8220;address3&#8221;: &#8220;Kettering&#8221;, &#8220;address4&#8221;: &#8220;Northants&#8221;, &#8220;comp_name&#8221;: &#8220;Orion Vehicles Leasing&#8221;, &#8220;contid&#8221;: 1, &#8220;email&#8221;: &#8220;&#8221;, &#8220;email2&#8221;: &#8220;&#8221;, &#8220;fax&#8221;: &#8220;&#8221;, &#8220;firstname&#8221;: &#8220;David John&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-825","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=825"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/825\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}