JSON过滤制表符

使用NSJSONSerialization解析JSON的时候,出现错误码为3840,信息Unescaped control character around character,服务器返回的数据包含了无法解析的字符,这时候得处理下特殊字符。如下:

/* 使用空格代替控制符等特殊字符 */
- (NSString *)stringByReplaceControlCharacters:(NSString *)inputString
{
    NSMutableCharacterSet *controlChars = [NSMutableCharacterSet controlCharacterSet];
    [controlChars removeCharactersInString:@"\n"];
    NSRange range = [inputString rangeOfCharacterFromSet:controlChars];
    
    NSMutableString *mutableString = [NSMutableString stringWithString:inputString];
    if (range.location != NSNotFound) {
        while (range.location != NSNotFound) {
            [mutable replaceCharactersInRange:range withString:@" "];
            range = [mutable rangeOfCharacterFromSet:controlChars];
        }
    }
    
    return mutableString;
}

你可能感兴趣的:(JSON过滤制表符)