5

我在服务器上注册设备数据,以获取推送通知。这里是代码,

[NSURLConnection sendAsynchronousRequest: request
                                           queue: _postQueue
                               completionHandler: ^(NSURLResponse *response, NSData *responseData, NSError *connectionError) {
                                   if (connectionError) {
                                       //
                                   } else {
                                      NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableContainers error: &error];
                                   }
                               }];

我收到错误

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=0x17057f60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

有人可以帮忙解决这个问题吗?

4

2 回答 2

7

错误消息准确地告诉您出了什么问题:来自您的服务器的响应不包含有效的 JSON。从技术上讲,JSON 必须以数组或对象(字典)开头。无论您的服务器返回什么都不是。NSJSONReadingAllowFragments无论使用该选项,您都可以强制使用 JSON 。

如果在使用该选项后仍然出现错误,那么您的服务器可能返回格式错误的 JSON(或根本没有 JSON)。你为什么不看看你的服务器的日志,看看你发回的究竟是什么?

于 2015-01-13T06:10:04.183 回答
0

我有同样的问题看看我在这里做了什么

我已将我的 json 解析方法更改为

let decodedApps = try JSONDecoder().decode(class.self, from: data!)

这里的类包含 json 数据键中存在的所有键,现在您可以将 decodedApps 用作字典.....其中包含键值对可能会有所帮助

于 2018-06-20T06:02:18.833 回答