Ich versuche, JSON-Daten in Swift 3.0-Format zu konvertieren, aber ich erhalte einen Fehler.JSON Parsing schlägt in Swift 3.0 fehl
Hier ist meine JSON-Daten:
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/fJgYDRLJbQIA4cQD71Hu-VtHYuM\"",
"id": {
"kind": "youtube#video",
"videoId": "diVd_vpuons"
},
"snippet": {
"publishedAt": "2015-07-16T13:47:55.000Z",
"channelId": "UC1iwdTRSV1Z2DMopttg8ocA",
"title": "Bengali Pala Kirtan | New Bhajan Kirtan | 2015 | Sabitri Satyaban | Shanta Das | Gold Disc",
"description": "Watch The New Bengali Pala Kirtan By Shanta Das \"Sabitri Satyaban \". Song : Sabitri Satyaban Album : Sabitri Satyaban Singer : Shanta Das Music By ...",
"thumbnails": {
"default": {
"url": "",
"width": 120,
"height": 90
},
"medium": {
"url": "",
"width": 320,
"height": 180
},
"high": {
"url": "",
"width": 480,
"height": 360
}
},
"channelTitle": "RDC Banglar Geeti",
"liveBroadcastContent": "none"
}
},
Und das ist mein Swift Code:
func getChannelDetails(_ useChannelIDParam: Bool) {
var urlString: String!
if !useChannelIDParam {
urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=PLbhFoUkf_GY6us_3RYg7U_NeNbqlc2AXY&key=AIzaSyAop5T2uSqj4Mw9nAE740za7mAHHiRwO2M"
}
let targetURL = URL(string: urlString)
performGetRequest(targetURL, completion: { (data, HTTPStatusCode, error) -> Void in
if HTTPStatusCode == 200 && error == nil {
do {
// Convert the JSON data to a dictionary.
let resultsDict = try JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<String, Any>
// Get all playlist items ("items" array).
let items: Array<Dictionary<String, Any>> = resultsDict["items"] as! Array<Dictionary<String, Any>>
// Use a loop to go through all video items.
for i in 0 ..< items.count {
var desiredValuesDict: Dictionary<String, Any> = Dictionary<String, Any>()
let objDicit = (items[i] as Dictionary<String, Any>)["id"] as! Dictionary<String, Any>
desiredValuesDict["videoID"] = (objDicit["videoID"] as! Dictionary<String, Any>) ["videoId"]
let snippetDict = (items[i] as Dictionary<String, Any>)["snippet"] as! Dictionary<String, Any>
// Get the snippet dictionary that contains the desired data.
// Create a new dictionary to store only the values we care about.
desiredValuesDict["title"] = snippetDict["title"]
desiredValuesDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<String, Any>)["high"] as! Dictionary<String, Any>)["url"]
//desiredValuesDict["videoID"] = (snippetDict["resourceId"] as! Dictionary<String, Any>) ["videoId"]
// Save the channel's uploaded videos playlist ID.
// Append the desiredValuesDict dictionary to the following array.
self.channelsDataArray.append(desiredValuesDict)
// Reload the tableview.
self.tblVideos.reloadData()
// Load the next channel data (if exist).
}
} catch {
print(error)
}
} else {
print("HTTP Status Code = \(HTTPStatusCode)")
print("Error while loading channel details: \(String(describing: error))")
}
self.viewWait.isHidden = true
})
}
Der Titel und Bild dargestellt sind, aber ich bin den folgenden Fehler erhalten:
thread signal sigabrt xcode
ich würde Ihnen empfehlen Dritte Parser zu verwenden oder mit 'wenn let' Syntax Tieftauchgang, der erste ist sehr schön und macht das Leben und Code einfach https://stackoverflow.com/a/41037513/3535583 – Mukesh