我收到服务器的回复,其中包含我要迭代的节目日期,我该怎么做。问题是关键"实例"具有该对象的值,该对象包含我要存储的日期的键值对。请帮忙
这是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()
答案 0 :(得分: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)
}