数组中的数组Swift 4 Json

数组中的数组Swift 4 Json,json,swift,Json,Swift,想要在三个单独的数组中获得缩写,缩写,分钟。因此根是一个单独的元素,如中的key1=xml和key2=root。 这就是我所拥有的。我得到了一个数组,但不能得到我前面提到的值 ["?xml": { "@encoding" = "utf-8"; "@version" = "1.0"; }, "root": { "@id" = 1; date = "11/25/2018"; message = ""; station = (

想要在三个单独的数组中获得缩写,缩写,分钟。因此根是一个单独的元素,如中的key1=xml和key2=root。 这就是我所拥有的。我得到了一个数组,但不能得到我前面提到的值

["?xml": {
    "@encoding" = "utf-8";
    "@version" = "1.0";
}, "root": {
    "@id" = 1;
    date = "11/25/2018";
    message = "";
    station =     (
                {
            abbr = 24TH;
            etd =             (
                                {
                    abbreviation = ANTC;
                    destination = Antioch;
                    estimate =                     (
                                                {
                            bikeflag = 1;
                            color = YELLOW;
                            delay = 86;
                            direction = North;
                            hexcolor = "#ffff33";
                            length = 10;
                            minutes = 4;
                            platform = 2;
                        },
                                                {
                            bikeflag = 1;
                            color = YELLOW;
                            delay = 0;
                            direction = North;
                            hexcolor = "#ffff33";
                            length = 10;
                            minutes = 23;
                            platform = 2;
                        },
                                                {
                            bikeflag = 1;
                            color = YELLOW;
                            delay = 0;
                            direction = North;
                            hexcolor = "#ffff33";
                            length = 10;
                            minutes = 43;
                            platform = 2;
                        }
                    );
                    limited = 0;
                },
                                {
                    abbreviation = DALY;
                    destination = "Daly City";
                    estimate =                     (
                                                {
                            bikeflag = 1;
                            color = BLUE;
                            delay = 214;
                            direction = South;
                            hexcolor = "#0099cc";
                            length = 9;
                            minutes = 11;
                            platform = 1;
                        },
                                                {
                            bikeflag = 1;
                            color = BLUE;
                            delay = 0;
                            direction = South;
                            hexcolor = "#0099cc";
                            length = 9;
                            minutes = 27;
                            platform = 1;
                        },
                                                {
                            bikeflag = 1;
                            color = BLUE;
                            delay = 0;
                            direction = South;
                            hexcolor = "#0099cc";
                            length = 9;
                            minutes = 47;
                            platform = 1;
                        }
                    );
                    limited = 0;
                },
                                {
                    abbreviation = DUBL;
                    destination = "Dublin/Pleasanton";
                    estimate =                     (
                                                {
                            bikeflag = 1;
                            color = BLUE;
                            delay = 0;
                            direction = North;
                            hexcolor = "#0099cc";
                            length = 9;
                            minutes = 10;
                            platform = 2;
                        },
                                                {
                            bikeflag = 1;
                            color = BLUE;
                            delay = 0;
                            direction = North;
                            hexcolor = "#0099cc";
                            length = 9;
                            minutes = 30;
                            platform = 2;
                        },
                                                {
                            bikeflag = 1;
                            color = BLUE;
                            delay = 0;
                            direction = North;
                            hexcolor = "#0099cc";
                            length = 9;
                            minutes = 50;
                            platform = 2;
                        }
                    );
                    limited = 0;
                },
                                {
                    abbreviation = MLBR;
                    destination = "SFO/Millbrae";
                    estimate =                     (
                                                {
                            bikeflag = 1;
                            color = YELLOW;
                            delay = 245;
                            direction = South;
                            hexcolor = "#ffff33";
                            length = 10;
                            minutes = 17;
                            platform = 1;
                        },
                                                {
                            bikeflag = 1;
                            color = YELLOW;
                            delay = 254;
                            direction = South;
                            hexcolor = "#ffff33";
                            length = 10;
                            minutes = 38;
                            platform = 1;
                        },
                                                {
                            bikeflag = 1;
                            color = YELLOW;
                            delay = 0;
                            direction = South;
                            hexcolor = "#ffff33";
                            length = 10;
                            minutes = 53;
                            platform = 1;
                        }
                    );
                    limited = 0;
                }
            );
            name = "24th St. Mission";
        }
    );
    time = "05:28:01 PM PST";
    uri =     {
        "#cdata-section" = "http://api.bart.gov/api/etd.aspx?cmd=etd&orig=24TH&json=y";
    };
}]

以下是操场表格中的代码:

static func singleRoute(_ station: String?, completionHandler: @escaping (SingleRouteModel?) -> Void) {
    let routeURL = URL(string: "http://api.bart.gov/api/etd.aspx?cmd=etd&orig=\(station ?? "12TH")&key=MW9S-E7SL-26DU-VV8V&json=y")

    var routeModel = SingleRouteModel()

    URLSession.shared.dataTask(with: routeURL!) { (data, response, error) in
        if error == nil {
            do {
                guard let todo = try JSONSerialization.jsonObject(with: data!, options:.mutableContainers)
                    as? [String: Any] else {
                        print("error trying to convert data to JSON")
                        return
                }
                print("todo = \(todo)")
                let root = todo["root"] as? [String: Any]
                let station = root?["station"] as? [[String: Any]]
                var etd: [[String : Any]]?
                var estimate: Any?

                for (_, value) in (station?.enumerated())! {
                    etd = value["etd"] as? [[String: Any]]
                }

                var estimates: [String: Any]? = [:]
                if let etd = etd {
                    for (key, value) in etd.enumerated() {
                        estimates?["\(key)"] = value
                    }
                } else {
                    completionHandler(nil)
                }

                if let estimate = estimates {
                    for (k, v) in estimate {
                        print(v)
                    }
                }

                completionHandler(routeModel)
            } catch  {
                print("error trying to convert data to JSON")
                return
            }
        } else {
            print("no data")
        }
    }.resume()
}

是的,我想我们很多人都知道如何获取这些值。你能给我举一个上面代码的例子吗?这看起来不像一个快速数组。这是什么类型的?通常,仅在Swift类型上打印
说明
值并不能提供足够的信息来轻松对类型进行反向工程。您需要解释这些值是什么。让我们看看您的示例。这不是一个代码编写服务。@matt只有在您真正理解数据结构的时候才使用Codable。JSONSerialization是剖析数据的一种好方法,我做到了。我的答案中的缩写和分钟都是数组。
import Foundation
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

struct Estimate: Codable {
    let minutes: String
}

struct ETD: Codable {
    let abbreviation: String
    let estimate: [Estimate]
}

struct Station: Codable {
    let abbr: String
    let etd: [ETD]
}

struct Root: Codable {
    let station: [Station]
}

struct SingleRouteModel: Codable {
    let root: Root
}

func singleRoute(_ station: String?, completionHandler: @escaping (SingleRouteModel?) -> Void) {

    let routeURL = URL(string: "http://api.bart.gov/api/etd.aspx?cmd=etd&orig=\(station ?? "12TH")&key=MW9S-E7SL-26DU-VV8V&json=y")

    URLSession.shared.dataTask(with: routeURL!) { (data, response, error) in
        if let data = data {
            let model = try? JSONDecoder().decode(SingleRouteModel.self, from: data)
            completionHandler(model)
        }
        else {
            completionHandler(nil)
        }
    }.resume()
}

singleRoute(nil, completionHandler: { model in
    guard let model = model else { print("failed"); return }
    let abbrs = model.root.station
        .map { $0.abbr }
    let abbreviations = model.root.station
        .flatMap { $0.etd }
        .flatMap { $0.abbreviation }
    let minutes = model.root.station
        .flatMap { $0.etd }
        .flatMap { $0.estimate }
        .map { $0.minutes }
    print("abbrs:", abbrs)
    print("abbreviations:", abbreviations)
    print("minutes:", minutes)
})