色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

json解析出錯誤

錢多多2年前12瀏覽0評論

json解析出錯誤?

1. 編碼問題。

2. 如果data是有數(shù)據(jù)的,但是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數(shù)據(jù)存在\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數(shù)據(jù)當中沒有 \n \r \t 等制表符,當后臺給出有問題時,我們需要對json數(shù)據(jù)過濾 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