我正在使用NSURLSession委托方式制作http get请求以从服务器获取json数据。我最终将我的NSURLSessionDataDelegate中收到的NSData传递给模型对象以更新数组,如下所示:
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
networkDelegate?.updateGameList(data)
dataTask.resume()
}
一旦数据进入updateGameList函数,如果我尝试使用NSJSONSerialization.JSONObjectWithData就会发生奇怪的事情。如果我不尝试使用NSJSONSerialization,只是将NSData转换为字符串,它看起来像我期待的正确响应,如下所示:
Optional([{"id":"a19610e4-675e-40b3-8335-085b61bfb0e3","name":"tttt","status":"PLAYING"},
{"id":"3be2e411-0086-46fd-8fc9-38d11831d0fb","name":"LWP TEST","status":"PLAYING"},
{"id":"b5d97d73-1ce2-4e5b-8b38-805835e2a21d","name":"asd","status":"PLAYING"},
{"id":"8a15575c-4c2c-4b46-ad5b-51b4b2680416","name":"MsGame","status":"WAITING"},
{"id":"b54531b8-5323-4630-929c-6eb2cfebde63","name":"423","status":"PLAYING"},
{"id":"5a7cfaa0-e2a4-41f6-bda2-a854a2d00a57","name":"4234","status":"PLAYING"}])
但是,一旦我尝试使用带有NSJSONSerialization.JSONObjectWithData的NSData来创建NSArray,它就会开始抛出错误,因为数据似乎在开头或结尾被截断,如下所示:
Optional([{"id":"a19610e4-675e-40b3-8335-085b61bfb0e3","name":"tttt","status":"PLAYING"},{"id":"3be2e411-0086-46fd-8fc9-38d11831d0fb","name":"LWP TEST","status":"PLAYING"},
{"id":"6d4e9731-61be-4191-bb9a-a30b6395a4a2","name":"RANDOMBOT","status":"PLAYING"},{"id":"c31b363c-f565-4f4a-a6f1-1ac219425f40","name":"Testament ","status":"PLAYING"},
{"id":"af14d8bc-37a0-4ec3-88de-ee364344d720","name":"Testament ","status":"PLAYING"},
{"id":"29439dd9-357d-445b-856c-39862e19c2fc","name":"Testament ","status":"PLAYING"},{"id":"cc29046f-4e80-422d-a103-5be175e799c9","name":"matt7","status":"PLAYING"},
{"id":"ff75c546-0e9b-4560-8efb-d0fa5be61cde","name":"u","status":"DONE"},{"id":"3d88df55-6f84-469c-a18e-27e463dc30eb","name":"test","status":"PLAYING"},{"id":"40eb1b13-21c3-4c8d-a379-e6b85329374b","name":"test","status":"PLAYING"},
{"id":"4e7519dd-79d3-4229-8d0e-47ca112dc08f","name":"test","status":"PLAYING"},{"id":"32ce49cc-17aa-47ca-8b9f-1c35dbdb78e6","name":"test","status":"PLAYING"},
{"id":"f5d5c961-17eb-421d-86b1-fbbadfb795da","name":"test","status":"PLAYING")
update game list error Error Domain=NSCocoaErrorDomain Code=3840
"Unexpected end of file while parsing object."
UserInfo={NSDebugDescription=Unexpected end of file while parsing object.}
这是updateGameList函数,当我用其中的JSONSerialization代码注释掉do-catch块时,dataString打印出正确的响应,当我取消注释它时,它会多次运行,几乎就像它正在尝试处理数据一次只有一个块而不是一次全部。如果我取出.AllowFragments选项,它告诉我把它放入,当我把它放入时,它会得到json数据的真正错误,无法正常启动或结束。
func updateGameList(gameListData:NSData) {
let dataString = NSString(data: gameListData, encoding: NSUTF8StringEncoding)
print(dataString)
do {
let gameList:NSArray = try NSJSONSerialization.JSONObjectWithData(gameListData, options: [.AllowFragments]) as! NSArray
}
catch {
print("update game list error \(error)")
}
}
答案 0 :(得分:1)
从文档中引用:
可以多次调用此委托方法,并且每次调用 仅提供自上次呼叫以来收到的数据。该应用程序是 负责在需要时积累这些数据。
所以您正在观察的是一个功能:数据是以块的形式接收的(很可能是因为原始服务器使用Transfer-Encoding: chunked
),因此您无法期望能够单独解析每个块 - 你必须从一个响应中累积所有块,然后你可以尝试解析它。