NSJSONSerialization导致EXC_BAD_ACCESS

Chr*_*nen 8 xcode json exc-bad-access nsjsonserialization ios6

目前我正在编写一个应用程序(目标iOS 6,启用了ARC),它使用JSON进行数据传输,使用Core Data进行持久存储.JSON数据由PHP脚本通过json_encode从MySQL数据库生成.

我的问题是,对于某些表中的数据,以下代码失败:

- (NSDictionary *)executeFetch:(NSString *)query
{
    NSURL *requesturl = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSError *dataError = nil;
    self.jsonData = [NSData dataWithContentsOfURL:requesturl options:kNilOptions error:&dataError];

    NSError *error = nil;
    self.jsonSerializationResult = [NSJSONSerialization JSONObjectWithData:self.jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];

    return self.jsonSerializationResult;

}
Run Code Online (Sandbox Code Playgroud)

该程序总是在EXC_BAD_ACCESS错误的位置崩溃,它表示self.jsonSerializationResult和Instruments说有一个Zombie被检测到.我知道这意味着我发送消息的一些对象是零,但我无法找到如何解决它...这就是乐器所说的:

#   Address Category    Event Type  RefCt   Timestamp   Size    Responsible Library Responsible Caller
0   0xa1b8a70   CFString (mutable)  Malloc  1   00:01.603.081   32  Foundation  -[NSPlaceholderMutableString initWithBytesNoCopy:length:encoding:freeWhenDone:]
1   0xa1b8a70   CFString (mutable)  Release 0   00:01.603.137   0   Foundation  newJSONValue
2   0xa1b8a70   CFString (mutable)  Zombie  -1  00:01.603.259   0   Foundation  newJSONString
Run Code Online (Sandbox Code Playgroud)

我的程序适用于除此之外的每个JSON输出:

{
   "termin":[
      {
         "termin_id":"17",
         "veranstaltung_id":"20",
         "beginn":"2012-09-28 17:00:00",
         "ende":"2012-09-28 18:00:00",
         "freie_pl\u00e4tze":null
      },
      {
         "termin_id":"18",
         "veranstaltung_id":"26",
         "beginn":"2012-09-28 19:00:00",
         "ende":"2012-09-28 20:00:00",
         "freie_pl\u00e4tze":null
      },
      {
         "termin_id":"19",
         "veranstaltung_id":"26",
         "beginn":"2012-09-28 21:00:00",
         "ende":"2012-09-28 22:00:00",
         "freie_pl\u00e4tze":null
      },
      {
         "termin_id":"20",
         "veranstaltung_id":"46",
         "beginn":"2012-09-28 19:00:00",
         "ende":"2012-09-28 20:00:00",
         "freie_pl\u00e4tze":null
      },
      {
         "termin_id":"24",
         "veranstaltung_id":"66",
         "beginn":"2012-09-28 22:00:00",
         "ende":"2012-09-28 22:30:00",
         "freie_pl\u00e4tze":"120"
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我想到了一些可能的错误来源,但似乎没有人负责:

  • jsonData或jsonSerializationResult可能是nil:它们不是
  • PHP生成了无效的JSON:使用验证器检查了它
  • null值:其他表没有问题

有人有想法吗?

use*_*136 11

看起来像是一个bug /缺点NSJSONSerialization.问题是由转义的unicode字符(freie_pl\u00e4tze而不是freie_plätze)引起的.你有两个选择 -

  1. 将转义的Unicode转换为真正的Unicode字符.试试这个SO答案
  2. 使用另一个JSON引擎,例如JSONKit.JSONKit也声称比表现更高效NSJSONSerialization.

  • iOS 7 中“NSJSONSerialization”的转义 unicode 字符已修复,请参阅 http://openradar.appspot.com/radar?id=2397401#ag9zfm9wZW5yYWRhci1ocmRyFAsSB0NvbW1lbnQYgICAgIDdwwgM (2认同)