
这是我从NSURLConnection-Delegate(连接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",... },"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数据包不完整但我不知道如何检查它或如何查找应该位于我的代码中的问题.
解决方法 你是否实现了NSURLConnectionDelagate的所有委托方法.看起来你正在从“ – (voID)connection:(NSURLConnection *)连接dIDReceiveData:(NSData *)data”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的实例.
希望它会有所帮助.
总结以上是内存溢出为你收集整理的ios – 将JSON NSData转换为NSDictionary全部内容,希望文章能够帮你解决ios – 将JSON NSData转换为NSDictionary所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)