在没有对象路径的情况下绑定解码 JSON

Tying to decode JSON without object path

我是 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: "Expected to decode Dictionary but found an array instead.", underlyingError: nil))

出于示例的目的,我删除了存储文件的网络 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 decodedData = try decoder.decode(SupplierList[0].self, from: supplierList)

运气不好。 感谢任何帮助

尝试:

let list = try decoder.decode([String].self, from: supplierList)

您没有显示 SupplierList,但您应该能够从该数组构建它。