json数据快速返回零

当我整合了restful api时,它返回错误

可选(错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(Cocoa错误3840.)”(JSON文本不开始与数组或对象和选项允许片段不设置。)UserInfo = 0x17046c680 {NSDebugDescription = JSON文本没有以数组或对象和选项开始,以允许片段没有设置。})

你能告诉我为什么会发生这种情况吗? 我迅速编码。 请检查以下代码行:

var jsonResponse = NSJSONSerialization.JSONObjectWithData(ihelper.responseData!, options: NSJSONReadingOptions.AllowFragments, error: &error) as AnyObject? as? NSArray 

如果你不知道它是以字典还是数组开头,那么最好通过在线JSON查看器来粘贴你的json响应并确定数据。

如果json以Dictionary开头(Dictioanry以json响应中的{….}开头),则需要使用

  if let jsonResponse: NSDictionary = NSJSONSerialization.JSONObjectWithData(ihelper.responseData!, options: NSJSONReadingOptions.AllowFragments, error: &error) as? NSDictionary { println("Response are\(jsonResponse)") } 

如果json以Array开始(数组以json响应中的[….]开始),则需要使用

  if let jsonResponse: NSArray = NSJSONSerialization.JSONObjectWithData(ihelper.responseData!, options: NSJSONReadingOptions.AllowFragments, error: &error) as? NSArray { println("Response are\(jsonResponse)") }