Decode JSON Swift 4



有人能说出代码出了什么问题吗?我正在尝试将来自服务器的 JSON 解析为变量以存储值。当我运行代码时,我没有收到任何错误。当我在解码数据后进行打印(用户(时,它没有返回任何内容。有人可以帮助我解决这个问题吗?

这是我用来从服务器检索数据的代码。

guard let url = URL(string: "my-url") else { return }
        let session = URLSession.shared
        let task = session.dataTask(with: url) { (data, _, _) in
            guard let data = data else { return }

            do {
                let user = try JSONDecoder().decode(User.self, from: data)
                print(user)
            } catch {}
        }
        task.resume()

来自服务器的 JSON 结果

{
"UserId": "55844ef7-3f05-4560-8b37-216df422ffb8",
"ContactsDto": [
    {
        "Id": null,
        "UserId": null,
        "FriendUserId": "e61d09f8-9aec-4035-b0be-c36abea2d82b",
        "FirstName": null,
        "LastName": null,
        "Email": "",
        "PhonePrefix": null,
        "Phone": "54943597",
        "InviteStatus": "pending",
        "UserStatus": null,
        "ErrorCode": 0,
        "ErrorMessage": null
    },
    {
        "Id": null,
        "UserId": null,
        "FriendUserId": "b2e0d6d7-d97c-475e-a2ab-71cb8091a7a0",
        "FirstName": null,
        "LastName": null,
        "Email": "",
        "PhonePrefix": null,
        "Phone": "207-7575",
        "InviteStatus": "pending",
        "UserStatus": null,
        "ErrorCode": 0,
        "ErrorMessage": null
    },
    {
        "Id": null,
        "UserId": null,
        "FriendUserId": "8f8d6061-3a69-4641-ac40-329a824ff4e1",
        "FirstName": null,
        "LastName": null,
        "Email": "",
        "PhonePrefix": null,
        "Phone": "58511968",
        "InviteStatus": "pending",
        "UserStatus": null,
        "ErrorCode": 0,
        "ErrorMessage": null
    },
    {
        "Id": null,
        "UserId": null,
        "FriendUserId": "40c1e461-eb98-4e18-9363-13cfa460fe7e",
        "FirstName": null,
        "LastName": null,
        "Email": "",
        "PhonePrefix": null,
        "Phone": "57864550",
        "InviteStatus": "accepted",
        "UserStatus": null,
        "ErrorCode": 0,
        "ErrorMessage": null
    }
],
"ErrorCode": 0,
"ErrorMessage": null

}

我的结构

struct User: Decodable {
let UserId: String?
let ContactsDto: [ContactsDto]?
let ErrorCode: Int?
let ErrorMessage: String?

}

struct ContactsDto : Decodable {
let Id : String?
let UserId : String?
let FriendUserId : String?
let FirstName : String?
let LastName : String?
let Email : String?
let PhonePrefix : String?
let Phone : String?
let InviteStatus : String?
let UserStatus : String?
let ErrorCode : String?
let ErrorMessage : String?

}

根据你的错误,正如乔希在评论中告诉你的那样,你的模型上的类型不正确。

例如,您的 ContactDto 对象之一是:

{
    "Id": null,
    "UserId": null,
    "FriendUserId": "e61d09f8-9aec-4035-b0be-c36abea2d82b",
    "FirstName": null,
    "LastName": null,
    "Email": "",
    "PhonePrefix": null,
    "Phone": "54943597",
    "InviteStatus": "pending",
    "UserStatus": null,
    "ErrorCode": 0,
    "ErrorMessage": null
},

ErrorCode类型是一个数字(它看起来像一个整数(,但在您的模型结构中是一个字符串?。

请检查每个字段以获取所有类型,然后修复模型并重试。

从查看您的代码来看,您尝试解码 JSON 的方式不正确。

您返回的不是用户数组,而是单个用户对象。

因此,您可以尝试以下操作:

let user = try! JSONDecoder().decode(User.self, from: data)

最新更新