我有以下的JSON作爲響應:無法解析JSON數組NSArray的斯威夫特2
[
{
"id_post": "1",
"id_type": "1",
"title": "I hffjj",
"body": "nothing at all",
"visitors": "0",
"extrabutton": "none",
"deviceid": "468af7f24ade50c9"
},
{
"id_post": "2",
"id_type": "1",
"title": "suxk my ",
"body": "sssusushshd",
"visitors": "0",
"extrabutton": "none",
"deviceid": "468af7f24ade50c9"
}
]
我試圖解析它作爲一個NSArray
如下所示:
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray {
print("Success: \(jsonResult)")
}
} catch let parseError {
print(parseError)
}
}
我總是得到錯誤:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
我在做什麼錯?
小點,但如果你把你的警衛改爲'guard let data = data else {....}'你可以在下面的代碼中刪除感嘆號。 – JeremyP