json解析出錯誤?
1. 編碼問題。
2. 如果data是有數據的,但是arr是nil,找到error:The operation couldn't be completed. (Cocoa error 3840.)NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];NSError *error = nil;NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);NSString *dataString = [[NSString alloc] initWithData:data encoding:enc];NSData *utf8Data = [dataString dataUsingEncoding:NSUTF8StringEncoding];NSArray *arr = [NSJSONSerialization JSONObjectWithData:utf8Data options:NSJSONReadingMutableContainers error:&error];
3. json非標準格式:(比如json數據存在\n \r \t 等制表符)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
NSError *error = nil;NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);NSString *dataString = [[NSString alloc] initWithData:data encoding:enc];//json數據當中沒有 \n \r \t 等制表符,當后臺給出有問題時,我們需要對json數據過濾 dataString = [dataString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""]; dataString = [dataString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; dataString = [dataString stringByReplacingOccurrencesOfString:@"\t" withString:@""]; NSData *utf8Data = [dataString dataUsingEncoding:NSUTF8StringEncoding];NSArray *arr = [NSJSONSerialization JSONObjectWithData:utf8Data options:NS