簡體   English   中英

如何解析對象的單個JSON數組?

[英]How to parse a single JSON array of objects?

我正在調用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

從每個對象中提取出來並將其附加到字符串數組中。

MovieCast結構包含2個數組,其中1個是Cast對象,而2個是Crew對象。

您可以創建一個具有兩個屬性( gendernameprofilePath )共享的協議Staff ,聲明兩種類型都符合該協議,然后將CastCrew對象數組組合成Staff數組。

然后,您可以將結果數組從數組中每個條目的profilePath值映射到字符串數組:

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

像這樣更改Cast和Crew:

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編輯器中鍵入此命令的,而我的語法很少“完美”。它可能需要進行一些小的清理。)

得到響應后,請在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類型的數組中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM