【问题标题】:NSJSONSerialization bug?NSJSON序列化错误?
【发布时间】:2013-02-16 10:34:09
【问题描述】:

过去 10 小时一直在尝试调试崩溃,最后,我将其简化为以下代码:

NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"payload" ofType:@"txt"]];
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

使用 NSZombieEnabled,这会使应用程序在第三行(解析发生的地方)崩溃并记录:

*** -[CFString retain]: message sent to deallocated instance 0x758afa0

payload.txt的内容是:

[
   {
      "created_at":"2013-02-15T23:46:02-05:00",
      "description":"Take out the Big Gun sounded simple enough, except the Strogg were waiting. You, and a few marines like you, are the lucky ones. You've made it down in one piece and are still able to contact the fleet. The Gravity Well, the Strogg's newest weapon in its arsenal against mankind, is operational. With the fleet around Stroggos, 5% of ground forces surviving, and that number dwindling by the second, your orders have changed: free your comrades. Destroy the Gravity Well.Minimum: A 100% Windows XP/Vista-compatible computer system",
      "developer":"id Software",
      "external_id":"2340",
      "id":745,
      "image":"http://cdn.steampowered.com/v/gfx/apps/2340/header_292x136.jpg",
      "is_subscribed":0,
      "link":"http://store.steampowered.com/app/2340/",
      "price":"4.99",
      "seller_id":2,
      "thumb":"http://media.steampowered.com/steamcommunity/public/images/apps/2340/5bd6e22ffdf72fdfb5ce2092fa50150de5fbb56f.jpg",
      "title":"Quake II: Ground Zero",
      "updated_at":"2013-02-15T23:46:02-05:00",
      "is_subscribed":0
   },
   {
      "created_at":"2013-02-15T23:45:59-05:00",
      "description":"Rage through 32 single player levels and 6 deathmatch levels of sheer terror and fully immersive sound and lighting. Arm yourself against the cannibalistic Ogre, fiendish Vore and indestructible Schambler using lethal nails, fierce Thunderbolts and abominable Rocket and Grenade Launchers.Minimum: A 100% Windows XP/Vista-compatible computer system",
      "developer":"id Software",
      "external_id":"2310",
      "id":742,
      "image":"http://cdn.steampowered.com/v/gfx/apps/2310/header_292x136.jpg",
      "is_subscribed":0,
      "link":"http://store.steampowered.com/app/2310/",
      "price":"9.99",
      "seller_id":2,
      "thumb":"http://media.steampowered.com/steamcommunity/public/images/apps/2310/e5bdf8dc7759c573fe525d45b69011f6a173a984.jpg",
      "title":"Quake",
      "updated_at":"2013-02-15T23:45:59-05:00",
      "is_subscribed":0
   }
]

这只是一个包含 2 个字典的数组。我不确定是什么导致了这个崩溃/这个 JSON 有什么问题。

更新删除数组中第一个对象中的"is_subscribed":0 可以避免崩溃。

【问题讨论】:

  • 如果你尝试使用 SBJSON 库,这很容易。
  • @Dilip 我不想使用 SBJSON。我想知道为什么 NSJSONSerialization 崩溃和/或我的 JSON 出了什么问题。
  • @Dilip 请不要给出不好的建议。
  • 可能您的 payload.txt 文件有缺陷(某些字符格式错误)。我尝试在我的机器上重新创建问题,并创建了 payload.txt 文件,其中包含从上面的文本框中粘贴的内容,并且它有效。 (iOS 6.1 模拟器)
  • @gardenofwine 我考虑了这一点,并做了你所做的。像您一样从我的这篇 SO 帖子中复制/粘贴 JSON,但仍然会崩溃,因此它不会是有缺陷的 payload.txt。你有 NSZombieEnabled 吗?我只会在使用 NSZonbieEnabled 或者禁用 NSZombieEnabled 之后(随机)在相对较长的时间后才会崩溃。

标签: ios objective-c cocoa-touch nsjsonserialization


【解决方案1】:

看起来 Apple 在他们的课程中处理此类情况的错误处理方面做得不好,因为你得到了崩溃而不是正常的 nil 结果和填充的错误变量。您的 json 数据和简约代码是 Apple 通常要求的正确错误报告。通过以下链接报告错误 - https://developer.apple.com/bugreporter/ 不要忘记在 zip 中附加项目作为它崩溃的证据。

【讨论】:

  • 您能否澄清一下这个重复变量背后的逻辑是什么?不确定您打算如何处理它们。
  • 问题不仅仅是任何重复的键,而是特别是“is_subscribed”字符串键。事实上,如果你在主数组中交换两个字典的顺序,它就不再崩溃......所以它不仅仅是重复的键。阅读主要帖子下的讨论以获取更多信息。 [ { "key":0, "key":0 } ] 不会导致此问题。如果您阅读主帖上的讨论,您会发现字典中重复键背后的逻辑......这是我在服务器端的错误。
  • 你是对的。我经历了一系列测试,似乎只有一个奇怪的行为。我认为您有一个很好的错误报告案例。这不是预期的行为,此类类不应在任何设置中崩溃。您可以通过以下链接提交报告 - developer.apple.com/bugreporter
【解决方案2】:

我遇到了这个问题,对我来说,问题是 JSON 中的重复键。我的密钥被命名为不同的 - 不是像你的情况那样的“is_subscribed”,而是“food_nutrients”。我敢打赌,iOS 6 会在任何重复的键上崩溃,而两者之间还有另一个键 - 而不仅仅是一些 cmets 建议的“is_subscribed”键。

我已确认任何名称的重复键都是问题所在。相关:https://stackoverflow.com/a/21148319/2030

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多