Why is my JSON.parse() failing on iPhone?

I am using Titanium for a mobile application. In the application, the server returns JSON data, which is then parsed by JSON.parse(). On Android, it works fine. I also double-checked it to make sure it's valid with http://jsonformatter.curiousconcept.com/

Here is my JSON data:

    {
   "email":"example@mail.com",
   "count":6,
   "0":{
      "id":"146996",
      "user_id":"25069",
      "item":"item1",
      "start_my_day":"none",
      "scheduled":"n",
      "calendar":"none",
      "start":"00000000T000000",
      "end":"00000000T000000",
      "added":"2011-11-30 06:55:47",
      "updated":"2011-11-30 06:55:47"
   },
   "1":{
      "id":"146988",
      "user_id":"25069",
      "item":"item2",
      "start_my_day":"none",
      "scheduled":"n",
      "calendar":"none",
      "start":"00000000T000000",
      "end":"00000000T000000",
      "added":"2011-11-30 06:52:20",
      "updated":"2011-11-30 06:52:20"
   }
   }

When i tried to check what i get with:

var response = JSON.parse(json, function (key, value) {
    Ti.API.debug('JSON: ' + key + ' <-> ' + value);
    return value;
});

it looks like the object "0" is not parsed as it's supposed to be, but its fields are made part of it's parent. Here's the output:

[DEBUG] JSON: email <-> example@mail.com
[DEBUG] JSON: count <-> 2
[DEBUG] JSON: id <-> 146996
[DEBUG] JSON: user_id <-> 25069
[DEBUG] JSON: item <-> item1
[DEBUG] JSON: start_my_day <-> none
[DEBUG] JSON: scheduled <-> n
[DEBUG] JSON: calendar <-> none
[DEBUG] JSON: start <-> 00000000T000000
[DEBUG] JSON: end <-> 00000000T000000
[DEBUG] JSON: added <-> 2011-11-30 06:55:47
[DEBUG] JSON: updated <-> 2011-11-30 06:55:47
[DEBUG] JSON: 0 <-> [object Object]
[DEBUG] JSON: id <-> 146988
[DEBUG] JSON: user_id <-> 25069
[DEBUG] JSON: item <-> item2
[DEBUG] JSON: start_my_day <-> none
[DEBUG] JSON: scheduled <-> n
[DEBUG] JSON: calendar <-> none
[DEBUG] JSON: start <-> 00000000T000000
[DEBUG] JSON: end <-> 00000000T000000
[DEBUG] JSON: added <-> 2011-11-30 06:52:20
[DEBUG] JSON: updated <-> 2011-11-30 06:52:20
[DEBUG] JSON: 1 <-> [object Object]
[DEBUG] JSON:  <-> [object Object]

From what i see.. it's not what it's supposed to return. I tried to enclose the count in quotes, to change the "0" to "10", but the parsing stays the same. If you need any more info, please let me know.

Thanks

Answers


JSON.parse(str, func) is called for each property recursively for formatting/replacement purposes. It is not failing, but you should not use it for your needs.

If you want to iterate over the object, you'd better parse the JSON regularly and use a loop:

var parsed = JSON.parse(json);

for(var key in parsed) {
    console.log(key, parsed[key]);

    for(var key2 in parsed[key]) {
        console.log("Nested: ", key2, parsed[key][key2]);
    }
}

Need Your Help

Vagrant/VirtualBox shared folders without vboxsf

linux vagrant virtualbox mmap

I'm working on a project that uses Vagrant (with a Linux guest) for the build environment. The build process relies on mmap() to build certain application-specific binaries. Unfortunately, vboxsf,

how to managing hibernate session in spring

java spring hibernate tomcat hibernate-session

i have problem with session close what impact during running this application and what if this goes in production.