【问题标题】:NSJSONSerialization returns EXC_BAD_ACCESSNSJSONSerialization 返回 EXC_BAD_ACCESS
【发布时间】:2013-09-10 14:28:21
【问题描述】:

我正在解析由 PHP 脚本使用 NSJSONSerialization 创建的 JSON 文件。 当我清除代码(产品 - 清除)时,它工作得很好。但是当我停止程序并在没有 Product - clear 的情况下再次构建它时,它会在 EXC_BAD_ACCESS 的这一行崩溃。 我使用 ARC。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:adresse]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
if (response == nil) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Connection" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
else {
    if (![NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]) {
        UIAlertView *jsonAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"JSON Parsingerror" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [jsonAlert show];
    }
    else {
        jsonData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];    

通过添加这个来解决由转义的 unicode 字符引起的问题

NSString *escaped = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSString *name = [NSString
                   stringWithCString:[escaped cStringUsingEncoding:NSUTF8StringEncoding]
                   encoding:NSNonLossyASCIIStringEncoding];
NSData *responseData = [name dataUsingEncoding:NSUTF8StringEncoding];
responseData = [responseData subdataWithRange:NSMakeRange(0, [responseData length]-1)];   

变化

jsonData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];    

jsonData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];    

导致 SIGABRT 崩溃。

我做错了什么?它在产品清除后第一次工作,但尽管使用了 ARC,但随后因 EXC_BAD_ACCESS 崩溃。

我的 JSON 看起来像这样

[{"line":"ABC","date":"2013-10-02","description":"H\u00e4hnchenbrust mit Calvadosso\u00dfe (1,2,4,8)","art":"type1"},{"line":"DEF","date":"2013-10-02","description":"Frika‌​dellen \u0084Polpette\u0093 (Rind) mit Sardellen und Tomaten (8)","art":"type1"},{"line":"ABC","date":"2013-10-03","description":"Salatteller mit Gem\u00fcseschnitzel (4,2,8)","art":"type2"},{"line":"ABC","date":"2013-10-27","description":"Nudel-H‌​ackfleisch-Pfanne (Rind) mit Schafsk\u00e4se (2,4)","art":"type1"}]

【问题讨论】:

  • known bugs in NSJSONSerialization 可能导致这样的事情。你的 JSON 是什么样的?
  • [{"line":"ABC","date":"2013-10-02","description":"H\u00e4hnchenbrust mit Calvadosso\u00dfe (1,2,4,8 )","art":"type1"},{"line":"DEF","date":"2013-10-02","description":"Frikadellen \u0084Polpette\u0093 (Rind) mit Sardellen und Tomaten (8)","art":"type1"},{"line":"ABC","date":"2013-10-03","description":"Salatteller mit Gem\u00fcseschnitzel (4,2, 8)","art":"type2"},{"line":"ABC","date":"2013-10-27","description":"Nudel-Hackfleisch-Pfanne (Rind) mit Schafsk\ u00e4se (2,4)","art":"type1"}]
  • 把它放在问题中,以便人类可以阅读它。
  • 有趣! NSURLConnection 返回“操作无法完成。(Cocoa 错误 3840。)”我在 Google 上搜索过,但到目前为止还没有找到正确的答案。产品清除是指 xcode。顶部栏中的“产品”,然后“清除”
  • 它仍然与 SIGABRT 一起崩溃。当我删除转义的 unicode 字符的解决方法时,它不会返回任何错误,但会在 NSJSONSerialization 行再次因 EXC_BAD_ACCESS 而崩溃

标签: objective-c json exc-bad-access nsjsonserialization


【解决方案1】:

如果你想替换那些转义序列,你可以使用CFStringTransform:

NSMutableString *escaped = [[NSMutableString alloc] initWithData:response encoding:NSUTF8StringEncoding];
CFStringTransform((__bridge CFMutableStringRef)escaped, NULL, CFSTR("Any-Hex/Java"), YES);
NSData *unescapedData = [escaped dataUsingEncoding:NSUTF8StringEncoding];

【讨论】:

    【解决方案2】:

    我想我解决了这个问题。我使用了一个单独的类 JSONParser.h /.m 一次又一次地构建。我现在直接使用父类中的代码进行解析,它正在工作。

    【讨论】:

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