解码器在解析后打乱 JSON 键包含动态键序列

问题描述

我有类似提到的动态 JSON 密钥 here in this thread 使用 decodeable,我必须从 json 生成对象。

struct ChannelSequence : Codable {
    public var innerArray: [String: InnerChannelNode]  
    
    private struct CustomCodingKeys: CodingKey {
        
        var stringValue: String
        init?(stringValue: String) {
            self.stringValue = stringValue
        }
        
        var intValue: Int?
        init?(intValue: Int) {
            return nil
        }
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CustomCodingKeys.self) 
        self.innerArray = [String: InnerChannelNode]()
        for key in values.allKeys {
            let value = try values.decode(InnerChannelNode.self,forKey: CustomCodingKeys(stringValue: key.stringValue)!)
            self.innerArray[key.stringValue] = value //<--------- here I lost the order 
        }
    }
    
}

但是,Keys 的顺序是随机排列的。

例如

当我检查 values.allKeys

来自 [151,152,153,154]

到 [154,151]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)