如何解决NSJSONSerialization结果为EXC_BAD_ACCESS
看起来像是一个错误/缺点NSJSONSerialization
。问题是由转义的unicode字符(freie_pl\u00e4tze
而不是freie_plätze
)引起的。您有两种选择-
解决方法
目前,我正在编写一个应用程序(使用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 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
我的程序可以处理除此以外的所有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",{
"termin_id":"19","beginn":"2012-09-28 21:00:00","ende":"2012-09-28 22:00:00",{
"termin_id":"20","veranstaltung_id":"46",{
"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:使用验证器进行了检查
- 空值:其他表没有问题
有人知道吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。