我想解析從新API返回的JSON字符串。返回的JSON字符串如下所示。我該如何解析這個使用Swift 3.0的JSON對象
QuerySearchResult":{
"StartAt":"1",
"Count":"40",
"TotalAvailable":"500",
"Items":[
{"TITLE":"OST420 Generation",
"PATH":"http:\\Test.pdf",
"WRITE":"2016-12-12T15:47:42",
"RANK":"32286574",
"SIZE":"145091",
"ISDOCUMENT":"true",
"ID":"18548",
"WPTASK":"Onsite Generation",
"WPDOCTYPE":"Local Operating Procedure",
"WPDOCREFID":"304580",
"WPCONTENTTYPE":"Document"},
{"TITLE":"OST420 Measurement",
"PATH":"http:\Test33.pdf",
.
.
我正在使用下面的代碼接受JSON變量,但失敗時,我嘗試加載項目。我試過使用Array
約Dictionary
但它仍然失敗。我需要在項目中讀取什麼聲明?
if let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as? Dictionary<String, AnyObject> {
for item in json {
if let dict = item as? Dictionary<String, AnyObject> {
if let items = json["Items"] as? Array<Dictionary<String, AnyObject>> {
for rec in items {
if let title = rec["TITLE"] as? String {
let xx = title
}
}
}
}
}
如果你不明白JSON是如何工作的,你可以通過打印的JSONObject開始 - 你命名爲「JSON」,並親眼看看它是如何存儲,從那裏你將獲得更清晰的圖片,瞭解如何訪問每個數據。 –
沒有日誌或任何東西?你怎麼知道哪一部分失敗呢? –