我正在解析一个使用 NSJSONSerialization 的 PHP 脚本创建的 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":"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"}]