简体   繁体   English

将 JSON NSData 转换为 NSDictionary

[英]Convert a JSON NSData to NSDictionary

I'm using an API service of a web service and it is written in their description that they send JSON data which also matches in my opinion with the response I get from it.我正在使用 Web 服务的 API 服务,并且在他们的描述中写道,他们发送的 JSON 数据在我看来也与我从中得到的响应相匹配。

Here a part of it which I got from the NSURLConnection-Delegate (connection didReceiveData: (NSData *) data) and converted in a NSString using:这是我从 NSURLConnection-Delegate (connection didReceiveData: (NSData *) data) 获得的一部分,并使用以下方法转换为 NSString:

NSLog(@"response: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

Here the snippet:这里的片段:

{"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": 

In the NSLog-String it stops there.在 NSLog-String 中,它停在那里。 Now I would like to know from you whats wrong that I can't convert this data to a JSON Dictionary with this lines of code:现在我想从您那里知道我无法使用以下代码行将此数据转换为 JSON 字典有什么问题:

NSError* error;
    NSMutableDictionary *dJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

I experiment with some options but always the same 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.}

Everything indicates that the JSON packet is incomplete but I don't know how to check it or how to look for the issue which should be located in my code.一切都表明 JSON 数据包不完整,但我不知道如何检查它或如何查找应位于我的代码中的问题。

Did you implemented all the delegate methods of NSURLConnectionDelagate.It looks like you are taking the data for conversion from "- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data" delagate method.您是否实现了 NSURLConnectionDelagate 的所有委托方法。看起来您正在从“- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data”delagate 方法中获取数据进行转换。 If so you may get incomplete data and that can not be converted.如果是这样,您可能会得到不完整的数据并且无法转换。

Try this:尝试这个:

- (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;
}

Here, lookServerResponseData is instance of NSMutableData declared globally.这里, lookServerResponseData是全局声明的 NSMutableData 实例。

Hope it will help.希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM