将JSON NSData转换为NSDictionary

8

我正在使用一个Web服务的API服务,根据他们的描述,他们发送JSON数据,这也与我从中得到的响应相匹配。

下面是我从NSURLConnection-Delegate (connection didReceiveData: (NSData *) data)获取的一部分内容,并使用以下方法将其转换为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-String 中,它停在那里。现在我想知道的是,为什么我无法使用这些代码将此数据转换为 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数据包不完整,但我不知道如何检查它或如何寻找问题,这个问题应该位于我的代码中。


2
看起来 API 发送了不完整或截断的数据。你无能为力。 - lead_the_zeppelin
2
你无法进行转换,因为它是不完整的。你是在didReceiveData:还是connectionDidFinishLoading:中尝试进行转换?如果是在didReceiveData中,那么如果HTTP将其分割,它将是不完整的,你必须在重新组装后的connectionDidFinishLoading中进行转换。 - Gruntcakes
是的,@MartinH,你说得对,我也像Rajeev在他的回答中写的那样做了!非常感谢你! - user3191334
+1 如果您实际使用错误参数并发布其报告。 - Hot Licks
1个回答

12

你是否已经实现了NSURLConnectionDelegate的所有委托方法?看起来你正在从“-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data”委托方法中获取数据进行转换。如果是这样,你可能会收到不完整的数据,那样数据就无法转换。

尝试这个:

- (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实例。
希望这会有所帮助。

哇!太感谢了!你说得对,这只是我收到的响应的一部分,我忘记使用其他委托,并将所有数据附加到最终的JSON中!我太高兴了哈哈 - 干得好! - user3191334

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接