如何解析单个JSON对象数组?

问题描述 投票:0回答:2

我正在调用API端点,JSON响应如下所示:

    {
        "id": 299536,
        "cast": [
            {
                "cast_id": 1,
                "character": "Tony Stark / Iron Man",
                "credit_id": "54a9cfa29251414d5b00553d",
                "gender": 2,
                "id": 3223,
                "name": "Robert Downey Jr.",
                "order": 0,
                "profile_path": "/1YjdSym1jTG7xjHSI0yGGWEsw5i.jpg"
            },
            {
                "cast_id": 6,
                "character": "Thor Odinson",
                "credit_id": "54a9d012c3a3680c29005762",
                "gender": 2,
                "id": 74568,
                "name": "Chris Hemsworth",
                "order": 1,
                "profile_path": "/lrhth7yK9p3vy6p7AabDUM1THKl.jpg"
            },
            {
                "cast_id": 13,
                "character": "Bruce Banner / Hulk",
                "credit_id": "573fc00592514177ec00010a",
                "gender": 2,
                "id": 103,
                "name": "Mark Ruffalo",
                "order": 2,
                "profile_path": "/isQ747u0MU8U9gdsNlPngjABclH.jpg"
            }
           ]

这是我为可编码协议所做的结构。

struct MovieCast: Codable {
    let id: Int
    let cast: [Cast]
    let crew: [Crew]
}

struct Cast: Codable {
    let castID: Int
    let character, creditID: String
    let gender, id: Int
    let name: String
    let order: Int
    let profilePath: String?

    enum CodingKeys: String, CodingKey {
        case castID = "cast_id"
        case character
        case creditID = "credit_id"
        case gender, id, name, order
        case profilePath = "profile_path"
    }
}

struct Crew: Codable {
    let creditID: String
    let department: Department
    let gender, id: Int
    let job, name: String
    let profilePath: String?

    enum CodingKeys: String, CodingKey {
        case creditID = "credit_id"
        case department, gender, id, job, name
        case profilePath = "profile_path"
    }
}

enum Department: String, Codable {
    case art = "Art"
    case camera = "Camera"
    case costumeMakeUp = "Costume & Make-Up"
    case crew = "Crew"
    case directing = "Directing"
    case editing = "Editing"
    case lighting = "Lighting"
    case production = "Production"
    case sound = "Sound"
    case visualEffects = "Visual Effects"
    case writing = "Writing"
}

我的问题是,如何使用Swift 4在单个数组中循环对象?我正在使用Decodable协议并且具有如下所示的结构:(请注意,我发布了上面的一些json响应属性,因此代码段不会太长)

我的目标是获得

profile_path

从每个对象中删除并将其附加到字符串数组中。

ios swift
2个回答
0
投票

收到回复后,请在viewDidLoad()中尝试此代码

if let dict = response.result.value as? [string:Anyobject]() //fetching the response
if let innerdict = dict["cast"] as? [[string:Anyobject]]() 

for cast in innerdict {
    if let profile_path = cast[ "profile_path"] as? String
    print("profile_path") // to check the profilepath

} //这是获取配置文件路径然后将其附加到string类型的数组中


0
投票

您的MovieCast结构包含2个数组,一个Cast对象和一个Crew对象。

您可以创建一个协议Staff,它具有两者的共享属性(gendernameprofilePath),声明两种类型符合该协议,然后将CastCrew对象的数组组合成Staff数组。

然后,您可以将结果数组映射到数组中每个条目的profilePath值的字符串数组:

protocol Staff {
  var gender: Int { get }
  var name: String { get }
  var profilePath: String? { get }
}

像这样改变施法者和船员:

struct Cast: Codable & Staff

struct Crew: Codable & Staff

如果你有一个船员:

var movieCrew: MovieCast
//Code to load a moview crew

然后

let castAndCrew = (movieCrew.cast as [Staff]) + ((movieCrew.crew as [Staff]?) ?? [])
let allProfilePaths = castAndCrew.compactMap { $0.profilePath }

(请注意,我将其输入到SO编辑器中,并且我的语法很难完美地“完成”。它可能需要进行一些小的清理。)

© www.soinside.com 2019 - 2024. All rights reserved.