IOS json 解析 IOS json 解析遇到错误问题解决办法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

IOS json 解析 IOS json 解析遇到错误问题解决办法

  2021-03-24 我要评论
想了解IOS json 解析遇到错误问题解决办法的相关内容吗,在本文为您仔细讲解IOS json 解析的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:IOS,json,解析,IOS,json,解析出错,IOS,json解析详解,下面大家一起来学习吧。

概要:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Unescaped control character around character 1419.) UserInfo=0x1563cdd0 {NSDebugDescription=Unescaped control character around character 1419.}

之前解析json的时候都是标准格式,json数据当中没有 \n \r \t 等制表符。

今天在解析的时候发现json解析时好时坏,用在线json解析也米有问题。找了半天终于发现是制表符在作怪,由于标准的json解析是不允许有这几个制表符的。所以在收到保温的时候我们需要把这几个制表符给过滤掉。

NSString * responseString = [request responseString];

responseString = [responseString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];

responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

responseString = [responseString stringByReplacingOccurrencesOfString:@"\t" withString:@""];

NSLog(@"responseString = %@",responseString);

SBJsonParser *parser = [[[SBJsonParser alloc]init] autorelease];

id returnObject = [parser objectWithString:responseString];

NSDictionary *userInfo = nil;

NSArray *userArr = nil;

if ([returnObject isKindOfClass:[NSDictionary class]]) {

if (userInfo) {

[userArr release];

}

userInfo = (NSDictionary*)returnObject;

}

else if ([returnObject isKindOfClass:[NSArray class]]) {

userArr = (NSArray*)returnObject;

}

NSError* e = nil;



//系统自带的解析方式。

NSDictionary * userInfo = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&e];

if (e) {

NSLog(@"%@",e);

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们