iOS SBJSON解析错误 Unescaped control character [0x09]

碰到这样的错误:JSONValue failed. Error is: Unescaped control character [0x09] 也就是说这段 json 中有特殊字符的意思,解决办法呢,就是把这些字符去掉,
有如下解决方案:
-(NSString *)removeUnescapedCharacter:(NSString *)inputStr
{

NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];

NSRange range = [inputStr rangeOfCharacterFromSet:controlChars];

  if (range.location != NSNotFound) 
  {

      NSMutableString *mutable = [NSMutableString stringWithString:inputStr];

      while (range.location != NSNotFound) 
      {

          [mutable deleteCharactersInRange:range];

          range = [mutable rangeOfCharacterFromSet:controlChars];

      }

      return mutable;

   }

  return inputStr;
}


Call this method with passing your output string like this
NSString *output = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"yourUrlString"] encoding:NSUTF8StringEncoding error:nil];

output = [self removeUnescapedCharacter:output];





在利用json-framework来实现json解析的过程时,会出现"-JSONValue Failed. Error is : Unescaped control character"的错误。这种问题一般是由转义字符引起的,这些转义字符可能是因为web服务编写时造成的,也可能是其它原因导致的。解决这一问题的关键代码如下。
[objc]  view plain copy
  1. NSString *jsonString = [[request responseString] stringByReplacingOccurrencesOfString:@"\r" withString:@""];  
  2. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];  
  3. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\s" withString:@""];  
  4. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""];  
  5. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\v" withString:@""];  
  6. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\f" withString:@""];  
  7. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\b" withString:@""];  
  8. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\a" withString:@""];  
  9. jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\e" withString:@""];  

    经过处理后的字符串再调用方法[jsonString JSONValue]来进行解析。

你可能感兴趣的:(iOS SBJSON解析错误 Unescaped control character [0x09])