-1

i am facing an issue in my project basically i want to print job listing in which user can see jobs that are available and also show required skills of each job (experience tags) here is my json result

{
"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"

            }
        ]
    }
]

}

i am unable to get experience tag

Usama Hassan
  • 81
  • 1
  • 8
  • Your `JSON` seems to be invalid. Are you sure this is the `JSON` you're trying to decode? Try to validate the `JSON` here https://jsonlint.com/. – Frankenstein Jun 16 '20 at 18:03
  • Did you create a struct model? Maybe you can share a bit of code so we can point out your error. Plus as @Frankenstein said, your JSON is invalid. Your experience_tags has an extra comma in it. The last element of the array shouldn't be followed by a comma. – rs7 Jun 16 '20 at 18:03
  • yes i create modal through this http://www.json4swift.com/ site – Usama Hassan Jun 16 '20 at 18:06

2 Answers2

0

I fixed the error for you. As I said there was an extra comma in your experience_tags array:

{
    "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"
    
                }
            ]
        }
    ]
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
rs7
  • 1,618
  • 1
  • 8
  • 16
0

Since you seem to have updated the JSON to valid structure you should be decoding the JSON incorrectly now. Here's the model and the decoding technique you need to decode this JSON successfully.

Model:

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"
    }
}

Decoding:

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)
}

Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • bro modal is not a problem. problem is that i am using the data array in order to show job listing and experience tags are basically skills for that particular job in my structure tableview show the data array and in same table view there is collection view for dynamic experience tags. i am able to get the data in tableview – Usama Hassan Jun 16 '20 at 18:31
  • let products = try JSONDecoder().decode(joblisting_modal.self, from: data) self.jobs_array = products.data! i did the same but want to get tags in seperated array like this let productsa = try JSONDecoder().decode(job_Data.self, from: products.data) self.experience_array = productsa.experience_tags! – Usama Hassan Jun 16 '20 at 18:41
  • You need to loop over the data using a for loop as I've done above. – Frankenstein Jun 16 '20 at 18:58