Parsen von JSON-Daten von alamofire in ein Array mit Dictionary

Ich versuche zu Parsen von JSON-Daten aus alamorefire wie folgt.

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, "https://api.mynexttrainschedule.net/")
            .responseJSON { response in
                guard let object = response.result.value else {
                    print("Oh, no!!!")
                    return
                }
                let json = JSON(object);print(json)
                let schedule = json[0]["schedule"]
        }
    }
}

Wenn ich drucken json, ich habe eine Daten-Struktur wie die folgende (bitte kurz und prägnant).

[
  {
    "schedule" : [
        {"departureTime" : "05:09", "destination" : "Boston", "trainType" : "Express"},
        {"departureTime" : "05:19", "destination" : "Portland", "trainType" : "Rapid"},
        {"departureTime" : "05:29", "destination" : "Boston", "trainType" : "Express""}
    ],
    "station" : "Grand Central",
    "direction" : "North"
  },
  {
    "schedule" : [
        {"departureTime" : "05:11","destination" : "Washington, "trainType" : "Express""},
        {"departureTime" : "05:23","destination" : "Baltimore, "trainType" : "Express""},
        {"departureTime" : "05:35","destination" : "Richmond, "trainType" : "Local""}
    ],
    "station" : "Grand Central",
    "direction" : "South"
  }
]

Nun, wie kann ich das speichern des Zeitplans array mit einem Wörterbuch (departureTime, Ziel...) durch oder nicht durch SwiftyJSON?

Dank.

UPDATE

Das folgende ist meine eigene Lösung.

import Alamofire
import SwiftyJSON

class ViewController: UIViewController {
    var scheduleArray = [Dictionary<String,String>]()

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, "https://api.mynexttrainschedule.net/")
            .responseJSON { response in
                guard let object = response.result.value else {
                    print("Oh, no!!!")
                    return
                }
                let json = JSON(object)
                if let jArray = json.array {
                    if let westHolidayArray = jArray[0]["schedule"].array {
                        for train in westHolidayArray {
                            if let time = train["departureTime"].string,
                                let dest = train["destination"].string,
                                let type = train["trainType"].string {
                                let dict = ["time":time, "dest":dest, "type": type]
                                self.scheduleArray.append(d)
                            }
                        }
                    }
                }
        }
    }
}

InformationsquelleAutor El Tomato | 2016-11-18

Schreibe einen Kommentar