绑定到没有对象路径的JSON解码
我是Swift的新手,有一个JSON文件,我想解码,它只是一个对象数组(指向其他JSON路径的字符串),没有对象定义: list.json的示例:绑定到没有对象路径的JSON解码,json,swift,decode,Json,Swift,Decode,我是Swift的新手,有一个JSON文件,我想解码,它只是一个对象数组(指向其他JSON路径的字符串),没有对象定义: list.json的示例: [ "filepath1.json", "filepath2.json", "filepath3.json", "filepath4.json", "filepath5.json", "filepath6.json", "filepath7.json", "filepath8.json", "filepath9.json" ] 尝试解码时,我收到一
[
"filepath1.json",
"filepath2.json",
"filepath3.json",
"filepath4.json",
"filepath5.json",
"filepath6.json",
"filepath7.json",
"filepath8.json",
"filepath9.json"
]
尝试解码时,我收到一个错误:
类型不匹配(Swift.Dictionary,Swift.DecodingError.Context(codingPath:[],debugDescription:“应解码字典,但找到了一个数组。”,underlineerror:nil))
在本例中,我删除了存储文件的web url(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 list = try decoder.decode([String].self, from: supplierList)
您没有显示SupplierList,但您应该能够从该阵列构造它。尝试:
您没有显示SupplierList,但应该能够从该数组构造它
let list = try decoder.decode([String].self, from: supplierList)