Unescaped control characters in NSJSONSerialization

Umair Khan Jadoon picture Umair Khan Jadoon · Jun 24, 2012 · Viewed 8.3k times · Source

I have this JSON http://www.progmic.com/ielts/retrive.php that I need to parse. When I do it with NSJSONSerialization, I get "Unescaped control character around character 1981" error.

I need to know:

  • What the heck are the unescaped control characters? Is there a list or something?
  • How do I get rid of this error? The easiest way?

Thanks in advance.

Answer

Umair Khan Jadoon picture Umair Khan Jadoon · Jun 25, 2012

I added this method to remove the unescaped characters from retrieved string:

- (NSString *)stringByRemovingControlCharacters: (NSString *)inputString 
{ 
    NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet]; 
    NSRange range = [inputString rangeOfCharacterFromSet:controlChars]; 
    if (range.location != NSNotFound) { 
        NSMutableString *mutable = [NSMutableString stringWithString:inputString]; 
        while (range.location != NSNotFound) { 
            [mutable deleteCharactersInRange:range]; 
            range = [mutable rangeOfCharacterFromSet:controlChars]; 
        } 
        return mutable; 
    } 
    return inputString; 
} 

After recieving the NSData, I convert it to NSString, call the above method to get a new string with removed control characters and then convert the new NSString to NSData again for further processing.