将JSON NSData转换为NSDictionary [英] Convert a JSON NSData to NSDictionary
问题描述
我正在使用Web服务的API服务,并在其描述中写道,他们发送的JSON数据也与我认为与之相对应. 这是我从NSURLConnection-Delegate(连接didReceiveData:(NSData *)数据)获得的一部分,并使用以下方法在NSString中进行了转换:
I'm using an API service of a web service and it is written in their description that they send JSON data wich also matches in my opinion with the response I get from it. Here a part of it which I got from the NSURLConnection-Delegate (connection didReceiveData: (NSData *) data) and converted in a NSString using:
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字典:
In the NSLog-String it stops there. 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:
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.}
一切都表明JSON数据包不完整,但我不知道如何检查它或如何查找应该位于我的代码中的问题.
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.
推荐答案
您是否实现了NSURLConnectionDelagate的所有委托方法.看起来您正在从-(void)connection:(NSURLConnection *)connection"进行转换数据didReceiveData:(NSData *)data"延迟方法.如果是这样,您可能会得到不完整的数据,并且无法转换.
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. If so you may get incomplete data and that can not be converted.
尝试一下:
- (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的实例.
Here, lookServerResponseData is instance of NSMutableData declared globally.
希望这会有所帮助.
这篇关于将JSON NSData转换为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!