错误域=NSCocoaErrorDomain Code=3840 "JSON文本没有以数组或对象开始,并且选项


Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set."

我已经使用链接代码发送和获取json数据。

我使用以下代码修改了与web服务器同步数据的代码。

- (IBAction)syncTickets:(id)sender {
 [self postTest];
 [self getTest];
}

我已经重写getTest函数将数据保存到电话数据库中。我得到一个错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
(Cocoa error 3840.)" (JSON text did not start with array or object and option to allow 
fragments not set.) UserInfo=0x987b960 {NSDebugDescription=JSON text did not start
with array or object and option to allow fragments not set.
}

这只发生在我第一次同步的时候,然后它工作得很好

我已经给出了答案,

答案与上文相同

有可能,来自服务器的响应不包含有效的JSON。

从技术上讲,JSON对象必须以"array"或"object (dictionary)"开头。

所以,无论你的服务器返回的是什么,都不是。

并且,你可以通过使用NSJSONReadingAllowFragments选项来强制使用JSON。

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];