json转换问题

data = [str dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;

dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

NSLog(@"error === %@",error);

描述:json转换错误:Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 111." UserInfo={NSDebugDescription=Unescaped control character around character 111.}

原因:因为str中包含了制表符;


解决:

while ([str containsString:@"\r\n"]) {

str = [str stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];

}

while ([str containsString:@"\t"]) {

str = [str stringByReplacingOccurrencesOfString:@"\t" withString:@""];

}

while ([str containsString:@"\n"]) {

str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];

}

data = [str dataUsingEncoding:NSUTF8StringEncoding];

NSError *error;

dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

NSLog(@"error === %@",error);

你可能感兴趣的:(json转换问题)