Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绑定到没有对象路径的JSON解码_Json_Swift_Decode - Fatal编程技术网

绑定到没有对象路径的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" ] 尝试解码时,我收到一

我是Swift的新手,有一个JSON文件,我想解码,它只是一个对象数组(指向其他JSON路径的字符串),没有对象定义:

list.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)