前提・実現したいこと
Alamofire
、SwiftyJSON
を使い、下記JSONデータの"title"
を取得したいです。
ネストされており、参考記事とデータ構造が異なるため、取得方法が分からないでいます。
どなたかご教示いただけませんでしょうか?
JSONデータ
{ "result": true, "data": [ { "type": "タイプ1", "article": { "id": "123", "title": "タイトル1" } }, { "article": { "id": "456", "title": "タイトル2" }, "type": "タイプ2" } ] }
試したこと
下記の通りコードを書きましたが、nil
となってしましいます。
Swift
1func getArticles() { 2 Alamofire.request("url").validate().responseJSON { response in 3// print(response.result.value) 4 5 guard let object = response.result.value else { 6 return 7 } 8 let json = JSON(object) 9 json["data"].forEach { (_, json) in 10 let type = json["title"].string! //Unexpectedly found nil while unwrapping an Optional value 11 print(type) 12 } 13 } 14 }
参考記事
Swift3 + Alamofire4 + SwiftyJSONでJSONレスポンスをパースするサンプル
AlamofireとSwiftyJSONでAPIを叩くチュートリアル
追記
回答1件
あなたの回答
tips
プレビュー