欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-17 20:48:22
概要: error domain=nscocoaerrordomain code=3840 "the operation couldn't be completed. (...

概要:

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);

}

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