我想迭代我在服务器响应中收到的json的日期

问题描述 投票:-1回答:1

我收到服务器的响应,其中包含我要迭代的显示日期,我该怎么做。问题是键“实例”具有对象的值,该对象包含我想要存储的日期的键值对。请帮助

这是JSON响应:

{
    "data": [
        {
            "id": "XYFYyvu2bckt",
            "theater_id": "clzEVgztkcWB",
            "theater_audi_id": "N9iDgooiCW6N",
            "movie_lang_id": "B9x98d4IEpCt",
            "booking_start_date": null,
            "instances": {
                "2018-04-18": [
                    {
                        "id": "vpcr2c9N12tL",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-19": [
                    {
                        "id": "X9kPcU7SLzrC",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-20": [
                    {
                        "id": "pFnFtXO5jKWp",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-21": [
                    {
                        "id": "hQuBFADUHeyS",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-22": [
                    {
                        "id": "vzXzOZvvKf9F",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-23": [
                    {
                        "id": "kAhzwyqoVGF4",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-24": [
                    {
                        "id": "wVXRjq6LrZJm",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-25": [
                    {
                        "id": "6hA1qglyP1Hf",
                        "show_time": "20:30:00"
                    }
                ],
                "2018-04-26": [
                    {
                        "id": "NoLPN8RQNRXV",
                        "show_time": "20:30:00"
                    }
                ]
            }
        }
    ]

}

这是我尝试过的:

  _ = URLSession.shared.dataTask(with: request) { (Data, response, error) in
            if Data != nil{
                do{
                    let access = try JSONSerialization.jsonObject(with: Data!, options: []) as! [String:NSArray]
                    Completion(access)
                    for i in access["data"]!{
                        let a = i

                        let b = try JSONSerialization.data(withJSONObject: a as Any, options: .prettyPrinted)

                        let d = try JSONSerialization.jsonObject(with: b, options: []) as! [String: Any]
                        let e = d["instances"]

                        for (key, timeObj)  in d["instances"]{
                            print(key)
                        }

                    }
                }catch let e{
                    print(e)
                }
            }
            }.resume()
ios json swift4
1个回答
0
投票

你可以试试

let access = try JSONSerialization.jsonObject(with: Data!, options: []) as! [String:Any] 
if let data = access["data"] as? [[String:Any]] , let instances = data[0]["instances"] as? [String:Any] {
  print(instances)
}
© www.soinside.com 2019 - 2024. All rights reserved.