Json Swift值(forKeyPath-keyPath:String)在Linux上不可用

Json Swift值(forKeyPath-keyPath:String)在Linux上不可用,json,swift,linux,macos,keypaths,Json,Swift,Linux,Macos,Keypaths,我的主要目标是从JSON文件中提取一个值或一个由键路径标识的值数组。 在macOS上,我为此创建了以下方法 extension JSONDecoder { func decode<T: Decodable>(_ type: T.Type, from data: Data, keyPath: String) throws -> T { // Create Foundation object for the entire JSON data

我的主要目标是从JSON文件中提取一个值或一个由键路径标识的值数组。 在macOS上,我为此创建了以下方法

extension JSONDecoder
{
    func decode<T: Decodable>(_ type: T.Type, from data: Data, keyPath: String) throws -> T
    {
        // Create Foundation object for the entire JSON data
        let toplevel = try JSONSerialization.jsonObject(with: data)
        
        // Extract part identified by the given keyPath
        if let nestedJson = (toplevel as AnyObject).value(forKeyPath: keyPath)
        {
            // Create a data-object from that part
            let nestedJsonData = try JSONSerialization.data(withJSONObject: nestedJson)
            // Try to create an object of the given type from the data-object
            return try decode(type, from: nestedJsonData)
        }
        else
        {
            throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "Nested json not found for key path \"\(keyPath)\""))
        }
    }
}

不幸的是,函数
func-value(forKeyPath-keyPath:String)->Any?
在Linux下不可用,我现在正在寻找替代方法。

可能通过检查?你必须自己动手做吗?@Larme:我来看看。谢谢你。
if let categories = try JSONDecoder().decode([String].self, from: data, keyPath: keyPath)
{
}