1

I am using SBJson framework (also known as json-framework) for the iOS.

When parsing a certain JSON file, I am getting the following error: -JSONValue failed. Error is: Unescaped control character [0x09]'

I have used this framework many times and I am also parsing a very similar JSON file (that is even much longer) in that same app and it's working fine.

I tried throwing around a bunch of NSLogs and everything seems to be fine. Can someone please point me to what this error means, or at least how to go ahead in debugging such an error?

Here is the code that displays the error:

- (void)downloadSchedule:(NSString *)jsonString {

    // Get JSON feed URL and instantiate a dictionary object with its content
    NSDictionary *topDic = [jsonString JSONValue];

    NSLog(@"topDic count %d", [topDic count]);

topDic is showing a count of 0. The error is at the [jsonString JSONValue] line.

Thank you

4 Answers 4

8

I have a great solution for it. Apply this method for remove escaped characters.

-(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];
4
  • Please edit your answer and format the code to make it readable.
    – kleopatra
    Dec 6, 2012 at 10:54
  • No. The correct solution is to fix the source of the invalid JSON, not to work around it with hacks like this. Dec 12, 2012 at 14:55
  • But for valid JSON we can fix it on both PHP and I-Phone side for more secure JSON parsing.
    – chandan
    Dec 14, 2012 at 12:17
  • I just used this to solve my problem. How do I know which character was causing the issue? Because it was working fine up until a few days ago.
    – marciokoko
    Aug 30, 2013 at 2:54
3

I guess your file contain an unencoded tab (ascii 0x09) that should be replaced with \t according to the json grammar.

2

Have a look at http://www.json.org/ There are some characters that need to be escaped to be properly parsed by JSON. This is the cause. The file is not proper JSON.

0

If your file is having '\n' or '\r' kind of html qoutes then it may cause error in obj-c. You can add :

[jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br />"]

I was having same problem and solved using this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.