0

I am parsing a JSON file created by a PHP-Script using NSJSONSerialization. When I clear the code (Product - clear) it works perfectly. But when I stop the program and build it again without Product - clear, it will crash at this line with EXC_BAD_ACCESS. I use 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];    

Working around the problem caused by escaped unicode characters by adding this

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)];   

and changing

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

to

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

results in a SIGABRT crash.

What am I doing wrong? It WORKS for the first time AFTER product clear but then crashes with EXC_BAD_ACCESS despite using ARC.

My JSON looks like that

[{"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"}]
nlc13
  • 1
  • 1
  • There are [known bugs in NSJSONSerialization](http://stackoverflow.com/questions/12842481/nsjsonserialization-results-in-exc-bad-access) which can cause something like this. What does your JSON look like? – Jesse Rusak Sep 10 '13 at 14:30
  • [{"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"}] – nlc13 Sep 10 '13 at 14:35
  • Put it in the question so that humans can read it. – Fogmeister Sep 10 '13 at 14:41
  • Interesting! The NSURLConnection returns "The operation couldn’t be completed. (Cocoa error 3840.)" I googled that but have not found the right answer so far. Product clear refers to xcode. "Product" in the top bar and then "clear" – nlc13 Sep 10 '13 at 15:02
  • It still crashes with the SIGABRT. When I remove the work-around for the escaped unicode characters it returns no error but crashes with EXC_BAD_ACCESS at the NSJSONSerialization line again – nlc13 Sep 10 '13 at 15:07
  • Hmm, I'm unable to reproduce your problem that necessitates the conversion of the unicode escape sequences, but if you want to "unescape" that string, I don't think your roundtrip to CString is the way to do it. I'd use `CFStringTransform`. – Rob Sep 10 '13 at 16:57

2 Answers2

0

If you want to replace those escape sequences, you can use CFStringTransform:

NSMutableString *escaped = [[NSMutableString alloc] initWithData:response encoding:NSUTF8StringEncoding];
CFStringTransform((__bridge CFMutableStringRef)escaped, NULL, CFSTR("Any-Hex/Java"), YES);
NSData *unescapedData = [escaped dataUsingEncoding:NSUTF8StringEncoding];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

I guess I solved the problem. I used a separate class JSONParser.h /.m which was build again and again. I now just parse using the code directly from the parental class and it is working.

nlc13
  • 1
  • 1