我的JSON响应如下所示:
{
data: {
type: "collections",
id: "dc165b3f-3600-486f-aa0b-d4719b18bb61",
attributes: {},
relationships: {
shows: {},
slides: {
links: {
self: "https://stage-api.cde.com/networks/online/j/collections/dc165b3f-3600-486f-aa0b-d4719b18bb61/relationships/slides",
related: "https://stage-api.cde.com/networks/online/j/collections/dc165b3f-3600-486f-aa0b-d4719b18bb61/slides"
},
data: [
{
type: "slides",
id: "935d2313-4816-44a9-a65a-06a98af2d730"
},
{
type: "slides",
id: "daf02fda-b356-46df-b1d0-8c3212d826bf"
},
{
type: "slides",
id: "77bbb082-5976-450a-bb48-fda9d2a44e80"
}
]
},
videos: {},
collections: {}
},
links: {}
}
}
我需要获取id
数组下slides
节点中存在的所有data
的值。我使用的是 SwiftyJSON 框架。
非常感谢任何帮助。如果不是SwiftyJSON那么请建议其他选项。
答案 0 :(得分:1)
试试这个:
import SwiftyJSON
let json: JSON = ...
let data = json["data"]["relationships"]["slides"]["data"]
guard let dataArray = data.array else {
fatalError("data array NOT FOUND")
}
for child in dataArray {
guard let id = child["id"].string else {
fatalError("id NOT FOUND")
}
print("id: \(id)")
}