如何在 Swift 5 中解析这个嵌套的 json?
How to parse this nested json in Swift 5?
在iOS中,我如何解析这个JSON?
{
"status": 1,
"data": [
{
"month": "8-2019",
"jobs": [
{
"jobId": 4,
"jobTitle": "",
"jobDesc": "",
"jobDate": "26 Sep 2019",
"jobVenue": "Singapore",
"jobAccept": "N"
}
]
}
],
"message": "Success"
}
您可以使用 quicktype to create Codable classes. As a starting point you should read more about the Codable Protocol
这样的服务
你可以用下面的代码来解析这个JSON:
import Foundation
// MARK: - Root
struct Root: Codable {
let status: Int
let data: [Datum]
let message: String
}
// MARK: - Datum
struct Datum: Codable {
let month: String
let jobs: [Job]
}
// MARK: - Job
struct Job: Codable {
let jobID: Int
let jobTitle, jobDesc, jobDate, jobVenue: String
let jobAccept: String
enum CodingKeys: String, CodingKey {
case jobID = "jobId"
case jobTitle, jobDesc, jobDate, jobVenue, jobAccept
}
}
您可以使用此代码将 JSON 转换为对象:
let root= try? JSONDecoder().decode(Root.self, from: jsonData)
在iOS中,我如何解析这个JSON?
{
"status": 1,
"data": [
{
"month": "8-2019",
"jobs": [
{
"jobId": 4,
"jobTitle": "",
"jobDesc": "",
"jobDate": "26 Sep 2019",
"jobVenue": "Singapore",
"jobAccept": "N"
}
]
}
],
"message": "Success"
}
您可以使用 quicktype to create Codable classes. As a starting point you should read more about the Codable Protocol
这样的服务你可以用下面的代码来解析这个JSON:
import Foundation
// MARK: - Root
struct Root: Codable {
let status: Int
let data: [Datum]
let message: String
}
// MARK: - Datum
struct Datum: Codable {
let month: String
let jobs: [Job]
}
// MARK: - Job
struct Job: Codable {
let jobID: Int
let jobTitle, jobDesc, jobDate, jobVenue: String
let jobAccept: String
enum CodingKeys: String, CodingKey {
case jobID = "jobId"
case jobTitle, jobDesc, jobDate, jobVenue, jobAccept
}
}
您可以使用此代码将 JSON 转换为对象:
let root= try? JSONDecoder().decode(Root.self, from: jsonData)