NSJSONSerialization results in EXC_BAD_ACCESS

前端 未结 3 1770
予麋鹿
予麋鹿 2020-12-09 21:17

Currently I am writing an app (Target iOS 6, ARC enabled) that uses JSON for data transmission and Core Data for persistent storage. The JSON data is generated out of a MySQ

相关标签:
3条回答
  • 2020-12-09 22:03

    It looks like a bug/shortcoming with NSJSONSerialization. The problem is caused by the escaped unicode characters (freie_pl\u00e4tze instead of freie_plätze). You have two options -

    1. Convert the escaped Unicode to real Unicode characters. Try this SO answer
    2. Use another JSON engine, such as JSONKit. JSONKit also claims to be more performant than NSJSONSerialization.
    0 讨论(0)
  • 2020-12-09 22:06

    I know this question has been answered but I think some beginners may have the same issue as me and be brought to this question.

    The EXC_BAD_ACCESS message was caused by malformed JSON. As I had accidentally used the same name for an Object which causes issues when converting the JSON into a dictionary.

    Annoyingly it didn't bring up a formatting error. Here is an example of the JSON that caused the issue:

    "levels" : {
        "level1": {
            ....
        },
        "level1": {
            ... << All objects should have different names. This should be called level2.
        },
        "level3": {
            ...
        }
    

    To fix the issue I had to ensure that all objects of the same level had different names.

    0 讨论(0)
  • 2020-12-09 22:11

    Just tested NSJSONSerialization today. With iOS 7.1. It is working. No issue found. Looks like Apple fixed the issue.

    NSString* jsonString = @"{ \"freie_pl\\u00e4tze\":null}";
    
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *error = nil;
    NSDictionary* jsonSerializationResult = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];
    
    NSLog(@"%@", jsonSerializationResult);
    
    0 讨论(0)
提交回复
热议问题