美文网首页
JSON过滤制表符

JSON过滤制表符

作者: WeeverLu | 来源:发表于2016-10-15 21:36 被阅读118次

    使用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过滤制表符

          本文链接:https://www.haomeiwen.com/subject/sbroyttx.html