iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.__編程

來源:互聯網
上載者:User

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

    }


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.