NSJSONSerialization结果为EXC_BAD_ACCESS
目前,我正在编写一个应用程序(使用TargetiOS6,已启用ARC),该应用程序使用JSON进行数据传输,并使用CoreData进行持久存储。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;
}
程序始终在显示self.jsonSerializationResult的行上崩溃并显示EXC_BAD_ACCESS错误,而Instruments则显示检测到Zombie。我知道这意味着我向其发送消息的某个对象为nil,但是我无法找到解决方法。这就是Instruments要说的:
#   Address Category    Event Type  RefCt   Timestamp   Size    Responsible Library Responsible Caller0   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
我的程序可以处理除此以外的所有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"
      }
   ]
}
我想到了一些可能的错误源,但似乎没有人负责:
- jsonData或jsonSerializationResult可能为nil:不是
- PHP生成无效的JSON:使用验证器进行了检查
- 空值:其他表没有问题
有人知道吗?
回答:
看起来像是一个错误/缺点NSJSONSerialization。问题是由转义的unicode字符(freie_pl\u00e4tze而不是freie_plätze)引起的。您有两种选择-
- 将转义的Unicode转换为真实的Unicode字符。
- 使用另一个JSON引擎,例如JSONKit。JSONKit还声称比自己表现更好NSJSONSerialization。
以上是 NSJSONSerialization结果为EXC_BAD_ACCESS 的全部内容, 来源链接: utcz.com/qa/402015.html





