Ios 将JSON NSData转换为NSDictionary

Ios 将JSON NSData转换为NSDictionary,ios,objective-c,json,nsdictionary,nsdata,Ios,Objective C,Json,Nsdictionary,Nsdata,我使用的是一个web服务的API服务,它们的描述中写到,它们发送的JSON数据在我看来也与我从中得到的响应相匹配 这里是我从NSURLConnection委托(connection didReceiveData:(NSData*)数据)获得的一部分,并使用以下命令转换为NSString: NSLog(@"response: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 下面

我使用的是一个web服务的API服务,它们的描述中写到,它们发送的JSON数据在我看来也与我从中得到的响应相匹配

这里是我从NSURLConnection委托(connection didReceiveData:(NSData*)数据)获得的一部分,并使用以下命令转换为NSString:

NSLog(@"response: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
下面是代码片段:

{"scans": 
{
    "Engine1“: 
    {
        "detected": false, 
        "version": "1.3.0.4959", 
        "result": null, 
        "update": "20140521"
    }, 
    "Engine2“: 
    {
        "detected": false,
         "version": "12.0.250.0",
         "result": null,
         "update": "20140521"
    }, 
        ...
    },
    "Engine13": 
    {
         "detected": false,
         "version": "9.178.12155",
         "result": 
在NSLog字符串中,它停在那里。现在,我想从您那里了解一下,我无法使用以下代码行将此数据转换为JSON字典有什么问题:

NSError* error;
    NSMutableDictionary *dJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
我尝试了一些选项,但总是出现相同的错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" 
(Unexpected end of file while parsing object.) UserInfo=0x109260850 {NSDebugDescription=Unexpected end of file while parsing object.}

一切都表明JSON数据包不完整,但我不知道如何检查它,也不知道如何查找代码中应该存在的问题。

您是否实现了NSURLConnectionDelagate的所有委托方法。看起来您正在从“-(void)connection:(NSURLConnection*)获取数据进行转换连接didReceiveData:(NSData*)数据“delagate方法。如果是这样,您可能会得到不完整的数据,并且无法转换

试试这个:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // A response has been received, this is where we initialize the instance var you created
    // so that we can append data to it in the didReceiveData method
    // Furthermore, this method is called each time there is a redirect so reinitializing it
    // also serves to clear it
    lookServerResponseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to the instance variable you declared
    [lookServerResponseData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now
    NSError *errorJson=nil;
    NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData:lookServerResponseData options:kNilOptions error:&errorJson];

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

    [lookServerResponseData release];
    lookServerResponseData = nil;
}
这里,lookServerResponseData是全局声明的NSMutableData的实例


希望能有所帮助。

API似乎正在发送不完整或截断的数据。您无能为力。您无法转换它,因为它不完整。您正在尝试在didReceiveData:或connectionDidFinishLoading:中转换它吗?如果它在didReceiveData中,那么如果HTTP拆分了它,那么它将是不完整的,在重新组装它之后,您必须在connectionDidFinishLoading中转换它。是的@Martin您是对的,我也像Rajeev在他的回答中写的那样做了!多谢各位+1用于实际使用错误参数并发布其报告。哇!非常感谢你!你是对的,这只是我收到的响应的一部分,我忘记了使用其他代理并将所有数据附加到最终的JSON!我太高兴了,哈哈,干得好!