NSURLConnection : JSON text did not start with array or object and option to allow fragments not set

北城余情 提交于 2019-12-04 22:29:27

Set option value to NSJSONReadingAllowFragments instead of kNilOptions I have tested your JSON taking it in a local file

id json = [NSJSONSerialization JSONObjectWithData:contentOfLocalFile
                                                options:NSJSONReadingAllowFragments
                                                  error:&deserializingError];

NSLog the actual data that you are getting, not the string, and check the first bytes. JSONSerializer didn't find a { or a [ as the first character, so you probably have some zero bytes, or byte order marks, or some other invisible characters like that.

In addtion to Janmenjaya's answer i would like to add:-

I have faced this issue twice in different API's. The problem each time that i had was

  1. First time the response that i was receiving was not in correct format. Remember the format must always start with a "[" or "{". So that was corrected from backend.
  2. Secondly , i was trying to hit an URL which had a word "video" in it for ex http://www.xyz/video123.com and websites related to name video have been blocked in our office. Therefore ensure that the network that you are using has no such restrictions. Postman will show you correct response but in devices or simulators you will face issues.

Please ensure these cases also.

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