我如何通过 JSONDecoder() 从嵌套数组中获取数据

how can i get data from nested arrays through JSONDecoder()

我在我的项目中遇到了一个问题,基本上我想打印工作列表,用户可以在其中看到可用的工作,还可以显示每项工作所需的技能(经验标签) 这是我的 json 结果

{
"message": "candidate job listing",
"status": true,
"data": [
    {
        "id": 1,
        "company_name_shown_job": "xyz company",
        "job_title": "Android Developer",
        "job_description": null,
        "experience_tags": [
            {
                "id": 2,
                "employer_job_post_id": 1,
                "experience_in": "Android"

            },
            {
                "id": 3,
                "employer_job_post_id": 1,
                "experience_in": "Php"

            }
        ]
    },
    {
        "id": 2,
        "company_name_shown_job": "Abc company",
        "job_title": "Web Developer",
        "job_description": "<p>Lorem ipsum dolor sit amet,laborum.</p>",
        "experience_tags": [
            {
                "id": 4,
                "employer_job_post_id": 2,
                "experience_in": "Swift"

            },
            {
                "id": 5,
                "employer_job_post_id": 2,
                "experience_in": "Java"

            }
        ]
    }
]

}

我无法获得经验标签

我为您修正了错误。正如我所说,您的 experience_tags 数组中有一个额外的逗号:

{
    "message": "candidate job listing",
    "status": true,
    "data": [
        {
            "id": 1,
            "company_name_shown_job": "xyz company",
            "job_title": "Android Developer",
            "job_description": null,
            "experience_tags": [
                {
                    "id": 2,
                    "employer_job_post_id": 1,
                    "experience_in": "Android"
    
                },
                {
                    "id": 3,
                    "employer_job_post_id": 1,
                    "experience_in": "Php"
    
                }
            ]
        },
        {
            "id": 2,
            "company_name_shown_job": "Abc company",
            "job_title": "Web Developer",
            "job_description": "<p>Lorem ipsum dolor sit amet,laborum.</p>",
            "experience_tags": [
                {
                    "id": 4,
                    "employer_job_post_id": 2,
                    "experience_in": "Swift"
    
                },
                {
                    "id": 5,
                    "employer_job_post_id": 2,
                    "experience_in": "Java"
    
                }
            ]
        }
    ]
}

由于您似乎已将 JSON 更新为有效结构,因此您现在应该错误地解码了 JSON。这是成功解码此 JSON 所需的模型和解码技术。

型号:

struct Response: Codable {
    let message: String
    let status: Bool
    let data: [Job]
}

struct Job: Codable {
    let id: Int
    let companyNameShownJob, jobTitle: String
    let jobDescription: String?
    let experienceTags: [ExperienceTag]

    enum CodingKeys: String, CodingKey {
        case id
        case companyNameShownJob = "company_name_shown_job"
        case jobTitle = "job_title"
        case jobDescription = "job_description"
        case experienceTags = "experience_tags"
    }
}

struct ExperienceTag: Codable {
    let id, employerJobPostID: Int
    let experienceIn: String

    enum CodingKeys: String, CodingKey {
        case id
        case employerJobPostID = "employer_job_post_id"
        case experienceIn = "experience_in"
    }
}

解码:

do {
    let response = try JSONDecoder().decode(Response.self, from: data)
    for job in response.data {
        for experienceTag in job.experienceTags {
            print(experienceTag)
        }
    }
} catch {
    print(error)
}