在Swift 5中使用结构解析JSON和JSONDecoder

时间:2019-06-23 10:59:07

标签: ios json swift parsing jsondecoder

我从Rest API获得json格式的结果。现在,我想使用JSONDecoder解析此JSON,但我不太了解响应的结构。

为此,我已经尝试创建结构以获取“ FirstUser”的“名称”。

{  
   "User":[  
      {  
         "FirstUser":{  
            "name":"John"
         },
         "Information":"XY",
         "SecondUser":{  
            "name":"Tom"
         }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-1)

如果我使用以前的json创建模型 使用此链接[blog]:http://www.jsoncafe.com生成可编码结构或任何格式

模型

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