ios – 字符0,NSJSONSerialization周围的值无效

ios – 字符0,NSJSONSerialization周围的值无效,第1张

概述我从我的服务器上获取了一个有效的响应: - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the new data to the instance variable you declared //[_responseData appendData: 我从我的服务器上获取了一个有效的响应:
- (voID)connection:(NSURLConnection *)connection dIDReceiveData:(NSData *)data {    // Append the new data to the instance variable you declared    //[_responseData appendData:data];    Nsstring *responseBody = [[Nsstring alloc] initWithData:data enCoding:NSUTF8StringEnCoding];    NSLog(@"%@",responseBody);    if(data != NulL)    {        dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,(unsigned long)NulL),^(voID) {        NSError *error = nil;        //NSMutableArray *JsonArray = [[CJsONDeserializer deserializer] deserializeAsArray:[responseBody dataUsingEnCoding:NSUTF8StringEnCoding] error:&error];            NSMutableArray *JsonArray = [NSJsONSerialization JsONObjectWithData:data options:NSJsONReadingallowFragments error:&error];        if (error)        {            NSLog(@"JsONObjectWithData error: %@",error);            [delegate onErrorGetArrayFromServer];        }        else        [self parseJsON:JsonArray];        });    }    else    {        if([delegate respondsToSelector:@selector(onErrorGetArrayFromServer)])        {            [delegate onErrorGetArrayFromServer];        }    }}

回复如下:

[{"ID":"37","ID_estado":"1","ID_categoria":"1","nombre":"fer","email":"asd@gmail.com","fecha":"2014-07-16 11:25:00","observaciones":"as dasd asdasd sasd","latitud":"37.619636","longitud":"-4.318449","foto":"images\/default.jpg"},{"ID":"36","ID_categoria":"6","nombre":"Fernando","email":"","fecha":"2014-07-16 10:32:45","observaciones":"que","latitud":"37.6178690439634","longitud":"-4.3238141387701","foto":"images\/default.jpg"}]

它抛出了我的错误:

JsONObjectWithData error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation Couldn’t be completed. (Cocoa error 3840.)" (InvalID value around character 0.) UserInfo=0x9e0f610 {NSDeBUGDescription=InvalID value around character 0.}

我尝试了其他库(CJsON),它给我发错误:

JsONObjectWithData error: Error Domain=kJsONScannerErrorDomain Code=-202 "Could not scan array. Could not scan a value." UserInfo=0xa15c0e0 {snippet=!HERE>![{"ID":"37","ID_esta,location=0,NSLocalizedDescription=Could not scan array. Could not scan a value.,character=0,line=0}

我的服务器是一个REST服务器,我的AndroID应用程序运行良好.

_解决了_

感谢@Himanshu Joshi:

为什么要在dIDReceiveData中解析数据?数据未完全下载,您必须在那里附加数据.解析connectionDIDFinishLoading中的数据:委托方法 –

我解析了connectionDIDFinishLoading中的数据:一切都很顺利.

解决方法 我遇到了同样的问题,但这是因为我的初始网址不正确.

在解析JsON之前,需要检查响应.它可能会告诉你它没有找到.

此时,数据实际上可以包含404 HTML页面.

我使用这些方法来调试这个,并使用更简单的方法来处理Web请求:

// Asynchronous fetch the JsON and call the block when doneNSURLSessionDataTask *downloadTask =[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {    NShttpURLResponse *httpResponse = (NShttpURLResponse *) response;    if ([httpResponse statusCode]!=200)     {        // log [response  description]        // log [Nsstring stringWithUTF8String:[data bytes]]         return;    }    NSDictionary* JsonDic =             [NSJsONSerialization JsONObjectWithData:data                             options:NSJsONReadingallowFragments                             error:&error];    if (!JsonDic)     {        return;    }    // process the JsON}];[downloadTask resume];
总结

以上是内存溢出为你收集整理的ios – 字符0,NSJSONSerialization周围的值无效全部内容,希望文章能够帮你解决ios – 字符0,NSJSONSerialization周围的值无效所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1104259.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存