简体   繁体   English

为什么我的JSON.parse()在iPhone上失败?

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

I am using Titanium for a mobile application. 我正在将Titanium用于移动应用程序。 In the application, the server returns JSON data, which is then parsed by JSON.parse(). 在应用程序中,服务器返回JSON数据,然后由JSON.parse()进行解析。 On Android, it works fine. 在Android上,效果很好。 I also double-checked it to make sure it's valid with http://jsonformatter.curiousconcept.com/ 我还仔细检查了它,以确保它与http://jsonformatter.curiousconcept.com/有效。

Here is my JSON data: 这是我的JSON数据:

    {
   "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. 看起来对象“ 0”未按预期解析,但其字段已成为其父级的一部分。 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. 我试图将计数括在引号中,将“ 0”更改为“ 10”,但是解析保持不变。 If you need any more info, please let me know. 如果您需要更多信息,请告诉我。

Thanks 谢谢

JSON.parse(str, func) is called for each property recursively for formatting/replacement purposes. 为格式化/替换目的,递归地为每个属性调用JSON.parse(str, func) 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: 如果要遍历该对象,则最好定期解析JSON并使用循环:

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]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM