Для следующих JSON,
{"id":"libMovies","jsonrpc":"2.0","result":{"limits":{"end":75,"start":0,"total":1228},"movies":[{"art":{"fanart":"myfanart","poster":"myposter"},"file":"myfile","label":"mylable","movieid":"mymovieid","playcount":0,"rating":"myrating","thumbnail":"mythumbnail"}]}}
Codable
моделей, которые вам нужно использовать,
struct Root: Decodable {
let id, jsonrpc: String
let result: Result
}
struct Result: Decodable {
let limits: Limits
let movies: [Movie]
}
struct Limits: Decodable {
let end, start, total: Int
}
struct Movie: Decodable {
let art: Art
let file, label, movieid: String
let playcount: Int
let rating, thumbnail: String
}
struct Art: Decodable {
let fanart, poster: String
}
Анализ JSON data
примерно так,
do {
let response = try JSONDecoder().decode(Root.self, from: data)
print(response.result.movies.map({"file: \($0.file), label: \($0.label)"}))
} catch {
print(error)
}
Редактировать:
Чтобы сохранить фильмы отдельно, создайте переменную типа [Movie],
var movies = [Movie]()
Теперь при разборе сохраните response.result.movies
в созданном выше свойстве,
do {
let response = try JSONDecoder().decode(Root.self, from: data)
print(response.result.movies.map({"file: \($0.file), label: \($0.label)"}))
movies = response.result.movies
} catch {
print(error)
}