JSON Parse Error

前端 未结 4 2014
我寻月下人不归
我寻月下人不归 2021-01-24 17:56

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. E

相关标签:
4条回答
  • 2021-01-24 18:29

    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 讨论(0)
  • 2021-01-24 18:31

    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];
    
    0 讨论(0)
  • 2021-01-24 18:31

    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.

    0 讨论(0)
  • 2021-01-24 18:35

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

    0 讨论(0)
提交回复
热议问题