码迷,mamicode.com
首页 > 移动开发 > 详细

iOS解析JSON字符串报错Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."

时间:2016-08-17 13:51:51      阅读:777      评论:0      收藏:0      [点我收藏+]

标签:

将服务器返回的JSON string转化成字典时报错:

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."

仔细查找后在原来解析的基础上将"\"字符替换成""或"\\"后,解析成功。具体的解析代码如下:

- (NSDictionary *)parseJsonStringToNSDictionary:(NSString *)jsonString
{
    
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
   
    NSError *error2=nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error2];
    if ([dict isValid]) {
        return dict;
    }
    return nil;
}

 

iOS解析JSON字符串报错Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."

标签:

原文地址:http://www.cnblogs.com/wobuyayi/p/5779551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!