我在 SwiftUI 中反序列化 json 物件時遇到問題。我對 Swift 和處理 json 都比較陌生。
這是 json 物件的鏈接:
https://www.reddit.com/r/all.json
這是我的資料模型
import Foundation
struct first : Codable, Identifiable{
var id = UUID()
let data : second
}
struct second : Codable, Identifiable{
var id = UUID()
let children : [third]
}
struct third : Codable, Identifiable{
var id = UUID()
let data : forth
}
struct forth : Codable, Identifiable{
var id = UUID()
let title : String
let url_overridden_by_dest : String
}
這是我用來反序列化json物件的代碼
mport Foundation
import SwiftUI
class ViewModel : ObservableObject {
func fetch() {
guard let url = URL(string: "https://www.reddit.com/r/all.json") else {
return
}
URLSession.shared.dataTask(with: url) { (data, _, _) in
let posts = try! JSONDecoder().decode(first.self, from: data!)
}
.resume()
}
}
我得到的最遠的是以下錯誤
Expected to decode Array but found a dictionary instead.
如果我沒有提供足夠的細節,我深表歉意,對于這個問題的任何幫助將不勝感激。
uj5u.com熱心網友回復:
我看到您的可編碼物件存在多個問題。首先,讓url_overridden_by_dest屬性成為可選的,因為它不是所有的JSON物件都有。
使id屬性恒定以在解碼期間排除它并為編碼鍵添加列舉。
struct first : Codable, Identifiable {
let id = UUID()
let data : Second
enum codingKeys: String, CodingKey {
case data
}
}
struct Second : Codable, Identifiable {
let id = UUID()
let children : [Third]
enum codingKeys: String, CodingKey {
case children
}
}
struct Third : Codable, Identifiable {
let id = UUID()
let data: Forth
enum codingKeys: String, CodingKey {
case data
}
}
struct Forth : Codable, Identifiable {
let id = UUID()
let title : String
let url_overridden_by_dest : String?
enum codingKeys: String, CodingKey {
case title
case url_overridden_by_dest
}
}
func fetch() {
guard let url = URL(string: "https://www.reddit.com/r/all.json") else {
return
}
URLSession.shared.dataTask(with: url) { (data, _, _) in
let posts = try! JSONDecoder().decode(first.self, from: data!)
print(posts)
}
.resume()
}
fetch()
輸出
first(id: F866FA04-41AE-4EB8-A790-4BF564A17920, data: __lldb_expr_16.Second(id: D203B974-4BB8-4E16-9FF9-9620017C74A2, children: [__lldb_expr_16.Third(id: AE38D7D8-A32D-495E-A29F-7598006ECA4B, data: __lldb_expr_16.Forth(id: 3EAF0CAA-836E-406B-95BA-9CEEEEA5D0E3, title: "How this made me smile! Why representation matters", url_overridden_by_dest: Optional("https://i.redd.it/k9pb9042d9981.jpg"))), __lldb_expr_16.Third(id: 70FBDC78-C45D-49E2-8C21-E4A2BE230E29, data: __lldb_expr_16.Forth(id: 14B7DAC1-EC60-4DEA-9A2F-02FB7F5DA0BA, title: "Well, in his defense, he\'s a fucking idiot", url_overridden_by_dest: Optional("https://i.redd.it/gg1gyyofi9981.png"))), __lldb_expr_16.Third(id: 3B1AD0EB-1F01-42C8-8B7C-2C05A7CA47EA, data: __lldb_expr_16.Forth(id: 82146995-E8E4-49A1-B284-A0E757FF2A7B, title: "NASA workers making the selfie with James Webb telescope before launch.", url_overridden_by_dest: Optional("https://i.redd.it/o5jq1ajig8981.jpg"))), __lldb_expr_16.Third(id: 0B326E6E-DADF-4DE4-A11E-4F131270513F, data: __lldb_expr_16.Forth(id: 727C3AF3-151E-4125-A1C8-E5789AEE4636, title: "He wasn\'t ready.", url_overridden_by_dest: Optional("https://gfycat.com/tinycircularbellsnake"))), __lldb_expr_16.Third(id: 7750D83D-22D2-4A72-8CA8-9F277245EF7B, data: __lldb_expr_16.Forth(id: 113E0D14-43A9-451C-BF27-C9FC54D6F0CB, title: "Opening a $15,000 bottle of Petrus, 1961 with heated tools. This method is used to make sure that the cork stays intact.", url_overridden_by_dest: Optional("https://v.redd.it/7pnwlpigf8981"))), __lldb_expr_16.Third(id: 5CB01E5F-C0C2-49CE-97C7-984A8D577617, data: __lldb_expr_16.Forth(id: 458B66B3-72E6-4137-9D42-27AA2C55FB3F, title: "*checks notes* ??", url_overridden_by_dest: Optional("https://v.redd.it/gxui4vvwb8981"))), __lldb_expr_16.Third(id: 8956CC12-0DE9-4127-B76F-03073713042D, data: __lldb_expr_16.Forth(id: A7AA87FE-E6F8-4381-95A2-2ABD7D02D37B, title: "(chuckles) we\'re in danger", url_overridden_by_dest: Optional("https://i.redd.it/8ofq8u6ol8981.jpg"))), __lldb_expr_16.Third(id: BA3DE24B-1B06-4CE9-BA8B-0BE08643C21E, data: __lldb_expr_16.Forth(id: 30E9B17F-606F-4C04-B885-534EDFF88FEB, title: "What a brave boy!", url_overridden_by_dest: Optional("https://v.redd.it/oh12raxxj8981"))), __lldb_expr_16.Third(id: 78AD282A-255C-47C3-B28B-4F40182FC48D, data: __lldb_expr_16.Forth(id: A5190DBA-A835-4D7B-A8AB-47718B84F64C, title: "Immigration test", url_overridden_by_dest: Optional("https://v.redd.it/9u5xl65z78981"))), __lldb_expr_16.Third(id: 5576F732-F19A-40BB-9BA4-B94F1D59DAD8, data: __lldb_expr_16.Forth(id: 139D0F69-6418-4E24-BE44-DEFF94FE0558, title: "Mum can I have a burger", url_overridden_by_dest: Optional("https://i.redd.it/e1q0m8d7p8981.png"))), __lldb_expr_16.Third(id: 3527478A-89BF-4234-9EB7-E1E74D922911, data: __lldb_expr_16.Forth(id: 55E56319-377E-464C-ACCF-B07B56E6D96D, title: "Throwback to when Lewis took down pro wrestler Mistico prior to the 2015 Mexican GP", url_overridden_by_dest: Optional("https://gfycat.com/illlightheartedaustralianfreshwatercrocodile"))), __lldb_expr_16.Third(id: C42139C0-89CD-44E7-BB4A-9A6654EA949C, data: __lldb_expr_16.Forth(id: 3016CAC3-5ADB-4A08-953E-3A020ACEF830, title: "Pop-up memes by PaperPaul.", url_overridden_by_dest: Optional("https://gfycat.com/harmlessshowyflamingo"))), __lldb_expr_16.Third(id: 36B5F793-E23A-4F91-84CD-D3642CB28331, data: __lldb_expr_16.Forth(id: 57DD4A1B-8152-452F-AE40-858AEA0F763B, title: "keep your forked tongue behind your teeth", url_overridden_by_dest: Optional("https://i.redd.it/ya1enmw7v8981.jpg"))), __lldb_expr_16.Third(id: F3A39671-52B8-4361-9ED7-BD9F964A159E, data: __lldb_expr_16.Forth(id: A95FA99F-C4A3-450F-B9D2-5149A6747F2F, title: "Police officer intentionally damages car with door while performing a search.", url_overridden_by_dest: Optional("https://v.redd.it/88ctqnhjo7981"))), __lldb_expr_16.Third(id: 3F55982A-D0F0-4B2A-8C5C-EC1496CA392E, data: __lldb_expr_16.Forth(id: 78A3320C-6C51-4907-BC66-50C27C41A9A7, title: "What is a survival myth that is completely wrong and could get you killed?", url_overridden_by_dest: nil)), __lldb_expr_16.Third(id: BA261231-A1D8-43B8-9291-D0202C332D5D, data: __lldb_expr_16.Forth(id: 890631DA-4D85-4211-9DDB-EFD0334B1DA7, title: "me_irl", url_overridden_by_dest: Optional("https://v.redd.it/0licbkbwk8981"))), __lldb_expr_16.Third(id: FFF2D8A9-F0AD-4CF3-9EAF-6148FDC4C5D9, data: __lldb_expr_16.Forth(id: 7BE3702A-175D-40AB-BD2E-F936297C64AB, title: "Karens in a nutshell", url_overridden_by_dest: Optional("https://i.imgur.com/RPGSua6.jpg"))), __lldb_expr_16.Third(id: ECAAD4E4-F317-4B49-83D5-BE14783F1DD4, data: __lldb_expr_16.Forth(id: 7F52F87F-DA69-4F5A-BD9F-A2C5663B293E, title: "Not just one more", url_overridden_by_dest: Optional("https://i.redd.it/ijk4ajjdn7981.jpg"))), __lldb_expr_16.Third(id: CCD2001B-2DC6-47C3-8BE1-FA10662E8AC5, data: __lldb_expr_16.Forth(id: CDF2FA47-A1CB-4B1E-86B2-C2E0D2EF6945, title: "RAP CONTEST!! Hololive X Nijisanji X VOMS", url_overridden_by_dest: Optional("https://v.redd.it/28igaujaj8981"))), __lldb_expr_16.Third(id: 69300EDA-0907-45B5-997F-56C291528D81, data: __lldb_expr_16.Forth(id: 6E8A032E-7885-4B6B-BD6D-2078C6D54408, title: "A wholesome family", url_overridden_by_dest: Optional("https://i.redd.it/xazlesb3c7981.jpg"))), __lldb_expr_16.Third(id: 5695449D-8115-486B-849F-52D1A5144254, data: __lldb_expr_16.Forth(id: 8E8B75BC-10A1-411E-BA55-868A183A1BC9, title: "Just had to close my store because no one would answer there phones.", url_overridden_by_dest: nil)), __lldb_expr_16.Third(id: DD411247-0FB0-4B78-990B-6BA6987765C5, data: __lldb_expr_16.Forth(id: 05C88A4B-6327-436A-B105-27AE77234BB1, title: "Being there is what keeps us going.", url_overridden_by_dest: Optional("https://v.redd.it/3w3sst0oa7981"))), __lldb_expr_16.Third(id: 0816D46F-3539-447D-91A9-598EDBF9A27C, data: __lldb_expr_16.Forth(id: 423C528E-DFEC-4532-A9E7-1F9716DFB429, title: "This glitched clock.", url_overridden_by_dest: Optional("https://v.redd.it/9um3g5g4e7981"))), __lldb_expr_16.Third(id: 99865D58-501B-4E2F-A4E4-4003350C057A, data: __lldb_expr_16.Forth(id: 8B64FFB6-0AFE-4966-981A-D4A70C0D53D9, title: "Animal rescuer helps free squirrel from birdcage.", url_overridden_by_dest: Optional("https://v.redd.it/o3oullaxn8981"))), __lldb_expr_16.Third(id: 360DDC86-832D-4E9F-9C4A-7EDB02C42045, data: __lldb_expr_16.Forth(id: DEED7C81-4235-4FD4-A7FD-F3EF8238C033, title: "meirl", url_overridden_by_dest: Optional("https://i.redd.it/im64hvtfp7981.jpg")))]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401326.html
