我是Swift的完全新手,并且有一个我想解码的JSON文件,只将其对象数组(指向其他JSON路径的字符串)定位,没有对象定义:
list.json的示例:
[
"filepath1.json",
"filepath2.json",
"filepath3.json",
"filepath4.json",
"filepath5.json",
"filepath6.json",
"filepath7.json",
"filepath8.json",
"filepath9.json"
]
尝试解码时出现错误:
typeMismatch(Swift.Dictionary,Swift.DecodingError.Context(codingPath:[],debugDescription:“预期对Dictionary进行解码,但找到了一个数组。”,底层错误:无))]]]
出于示例目的,我删除了存储文件的网址(supplierURL)我正在使用的示例解码:
let supplierURL = "list.json" func getSuppliers() { let urlString = supplierURL performRequest(urlString: urlString) } func performRequest(urlString: String) { if let url = URL(string: urlString) { let session = URLSession(configuration: .default) let task = session.dataTask(with: url) { (data, response, error) in if error != nil { print(error!) return } if let safeData = data { self.parseJSON(supplierList: safeData) } } task.resume() } } func parseJSON(supplierList: Data){ let decoder = JSONDecoder() do { let decodedData = try decoder.decode([SupplierList].self, from: supplierList) print(decodedData) } catch { print(error) } }
我也尝试过:
let decodedData = try decoder.decode(Array<SupplierList>.self, from: supplierList)
和
let decodedData = try decoder.decode(SupplierList[0].self, from: supplierList)
没有运气。任何帮助表示赞赏😊
我是Swift的一名新手,有一个我想解码的JSON文件,仅将其对象数组(指向其他JSON路径的字符串)定位,没有对象定义:list.json的示例:...] >
尝试:
let list = try decoder.decode([String].self, from: supplierList)