Json解析报错: Error is : Unescaped control character...的解决方法

在利用json-framework来实现json解析的过程时,会出现"-JSONValue Failed. Error is : Unescaped control character"的错误。这种问题一般是由转义字符引起的,这些转义字符可能是因为web服务编写时造成的,也可能是其它原因导致的。。  

 

NSString *jsonString0 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File.text" ofType:nil] encoding:NSUTF8StringEncoding error:nil];
    
    NSLog(@"%@",jsonString0);
    

  //添加过滤
    NSString *jsonString = [jsonString0 stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\v" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\f" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\b" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\a" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\e" withString:@""];
    
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *error = nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    NSLog(@"%@",error);
    
    NSLog(@"%@",dict);

posted @ 2016-12-06 16:49  会飞的夜良  阅读(297)  评论(0编辑  收藏  举报