簡體   English   中英

解碼JSON Swift 4

[英]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?

}

通過查看代碼,您嘗試解碼JSON的方式不正確。

您不會返回一個User數組,而是一個User Object。

因此,您可以嘗試以下操作:

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

根據您的錯誤,並且正如Josh在評論中告訴您的那樣,模型上的類型不正確。

例如,您的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類型是數字(看起來像整數),但是在您的模型結構中是String?。

請檢查每個字段以獲取所有類型,然后修復模型並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM