使用结构在 Swift 5 中使用 JSON 解码器解析 JSON
Parse JSON with JSONDecoder in Swift 5 using structs
我从 Rest API 中得到 json 格式的结果。现在我想用 JSON 解码器解析这个 JSON 但我并不真正理解我的响应的结构。
为此,我已经尝试创建结构来获取 "FirstUser" 的 "name"。
{
"User":[
{
"FirstUser":{
"name":"John"
},
"Information":"XY",
"SecondUser":{
"name":"Tom"
}
Json
{
"User":[
{
"FirstUser":{
"name":"John"
},
"Information":"XY",
"SecondUser":{
"name":"Tom"
}
}
]
}
型号
// MARK: - Empty
struct Root: Codable {
let user: [User]
enum CodingKeys: String, CodingKey {
case user = "User"
}
}
// MARK: - User
struct User: Codable {
let firstUser: FirstUserClass
let information: String
let secondUser: FirstUserClass
enum CodingKeys: String, CodingKey {
case firstUser = "FirstUser"
case information = "Information"
case secondUser = "SecondUser"
}
}
// MARK: - FirstUserClass
struct FirstUserClass: Codable {
let name: String
}
解析
do {
let res = try JSONDecoder().decode(Root.self, from: data)
print(res.first?.firstUser.name)
} catch {
print(error)
}
如果我使用以前的 json 创建模型
使用此 link [博客]:http://www.jsoncafe.com 生成 Codable 结构或任何格式
型号
import Foundation
struct RootClass : Codable {
let user : [Users]?
enum CodingKeys: String, CodingKey {
case user = "User"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
user = try? values?.decodeIfPresent([Users].self, forKey: .user)
}
}
struct Users : Codable {
let firstUser : FirstUser?
let information : String?
let secondUser : SecondUser?
enum CodingKeys: String, CodingKey {
case firstUser = "FirstUser"
case information = "Information"
case secondUser = "SecondUser"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
firstUser = try? FirstUser(from: decoder)
information = try? values?.decodeIfPresent(String.self, forKey: .information)
secondUser = try? SecondUser(from: decoder)
}
}
struct SecondUser : Codable {
let name : String?
enum CodingKeys: String, CodingKey {
case name = "name"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
name = try? values?.decodeIfPresent(String.self, forKey: .name)
}
}
struct FirstUser : Codable {
let name : String?
enum CodingKeys: String, CodingKey {
case name = "name"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
name = try? values?.decodeIfPresent(String.self, forKey: .name)
}
}
解析
do {
let res = try JSONDecoder().decode(RootClass.self, from: data)
print(res?.user?.first?.firstUser?.name ?? "Yours optional value")
} catch {
print(error)
}
我从 Rest API 中得到 json 格式的结果。现在我想用 JSON 解码器解析这个 JSON 但我并不真正理解我的响应的结构。
为此,我已经尝试创建结构来获取 "FirstUser" 的 "name"。
{
"User":[
{
"FirstUser":{
"name":"John"
},
"Information":"XY",
"SecondUser":{
"name":"Tom"
}
Json
{
"User":[
{
"FirstUser":{
"name":"John"
},
"Information":"XY",
"SecondUser":{
"name":"Tom"
}
}
]
}
型号
// MARK: - Empty
struct Root: Codable {
let user: [User]
enum CodingKeys: String, CodingKey {
case user = "User"
}
}
// MARK: - User
struct User: Codable {
let firstUser: FirstUserClass
let information: String
let secondUser: FirstUserClass
enum CodingKeys: String, CodingKey {
case firstUser = "FirstUser"
case information = "Information"
case secondUser = "SecondUser"
}
}
// MARK: - FirstUserClass
struct FirstUserClass: Codable {
let name: String
}
解析
do {
let res = try JSONDecoder().decode(Root.self, from: data)
print(res.first?.firstUser.name)
} catch {
print(error)
}
如果我使用以前的 json 创建模型 使用此 link [博客]:http://www.jsoncafe.com 生成 Codable 结构或任何格式
型号
import Foundation
struct RootClass : Codable {
let user : [Users]?
enum CodingKeys: String, CodingKey {
case user = "User"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
user = try? values?.decodeIfPresent([Users].self, forKey: .user)
}
}
struct Users : Codable {
let firstUser : FirstUser?
let information : String?
let secondUser : SecondUser?
enum CodingKeys: String, CodingKey {
case firstUser = "FirstUser"
case information = "Information"
case secondUser = "SecondUser"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
firstUser = try? FirstUser(from: decoder)
information = try? values?.decodeIfPresent(String.self, forKey: .information)
secondUser = try? SecondUser(from: decoder)
}
}
struct SecondUser : Codable {
let name : String?
enum CodingKeys: String, CodingKey {
case name = "name"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
name = try? values?.decodeIfPresent(String.self, forKey: .name)
}
}
struct FirstUser : Codable {
let name : String?
enum CodingKeys: String, CodingKey {
case name = "name"
}
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
name = try? values?.decodeIfPresent(String.self, forKey: .name)
}
}
解析
do {
let res = try JSONDecoder().decode(RootClass.self, from: data)
print(res?.user?.first?.firstUser?.name ?? "Yours optional value")
} catch {
print(error)
}