NSJSONSerialization gives me error “Duplicate key”

眉间皱痕 提交于 2019-12-08 02:49:44

问题


I request a JSON response from a home controlling device:

NSData* responseData = [NSData dataWithContentsOfURL:url];

The JSON data in responseData is complete and valid. However, if I try

NSDictionary* dict = [NSJSONSerialization 
                          JSONObjectWithData:responseData                          
                          options:kNilOptions 
                          error:&error];
NSLog("Error: %@", error);

it gives me the following error:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Duplicate key for object around character 0.) UserInfo=0x6a8b8e0 {NSDebugDescription=Duplicate key for object around character 0.}

I searched the web but couldn't find anything helpful so far.


Edit: The JSON response it quite long, so instead of posting it here, please find it here.


回答1:


It looks like the JSON is fine by pasting it into http://jsonformat.com/ - maybe you found an Apple bug? Another way to try and validate would be to use a third-party iOS library and see if it gives you the same error? Say, TouchJSON?




回答2:


I get the same error, only at random when the app runs. When the NSJSONSerialization fails I've checked the json object it chocks on using a JSON Viewer. The JSON is always OK. Bugs in NSJSONSerialization when it's called several times?




回答3:


I got the same error. My JSON data passed JSON validator at http://jsonformatter.curiousconcept.com/ . However, I found two duplicated keys in my JSON (at the end of dictionary). After I removed those keys, NSJSONSerialization works smoothly.



来源:https://stackoverflow.com/questions/9859178/nsjsonserialization-gives-me-error-duplicate-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!