微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用扩展名解码 JSON 嵌套字典数组:可解码?

如何解决如何使用扩展名解码 JSON 嵌套字典数组:可解码?

我有这样的 JSON:

{
  "states" : [
    "C","A","B","Sink",],"symbols" : [
    "c","a","b"
  ],"transitions" : [
    {
      "with" : "c","to" : "B","from" : "C"
    },{
      "with" : "c","to" : "Sink",{
      "with" : "b","from" : "B"
    },"to" : "C","from" : "A"
    },{
      "with" : "a","to" : "A","from" : "A"
    }
  ],"initialState" : "A","finalStates" : [
    "B"
  ]
}

我无法解码 JSON 的 transitions 部分(我需要像苹果在 here 中那样解码)。 到目前为止我得到的是这个(评论部分是导致错误 typeMismatch(Swift.Dictionary,Swift.DecodingError.Context(codingPath: [],debugDescription: "Expected to decode Dictionary 但找到了一个数组。",underlyingError: nil)) )

public struct FiniteAutomata {
    let states:[String]
    let symbols:[String]
    let initialState:String
    let finalStates:[String]
    /*
    let with:[String]
    let from:[String]
    let to:[String]
     */

    enum CodingKeys: String,CodingKey {
        case states
        case symbols
        case initialState
        case finalStates
        case transitions
    }
    /*
    enum transitionInfoKeys: String,CodingKey{
        case with
        case to
        case from
    }*/
}

extension FiniteAutomata: Decodable {
    public init(from decoder: Decoder)throws{
        let decoderContainer = try decoder.container(keyedBy: CodingKeys.self)
        states = try decoderContainer.decode([String].self,forKey: .states)
        symbols = try decoderContainer.decode([String].self,forKey: .symbols)
        initialState = try decoderContainer.decode(String.self,forKey: .initialState)
        finalStates = try decoderContainer.decode([String].self,forKey: .finalStates)
        /*
        let nestedContainer = try decoderContainer.nestedContainer(keyedBy: transitionInfoKeys.self,forKey: .transitions)
        with = try nestedContainer.decode([String].self,forKey: .with)
        to = try nestedContainer.decode([String].self,forKey: .to)
        from = try nestedContainer.decode([String].self,forKey: .from)
         */
    }
}

解决方法

transition 的值是一个字典数组,它变成了一个结构体数组

struct Transition : Decodable {
   let with,to,from : String
}

然后解码

let transitions : [Transition]

不需要所有 CodingKeys 和自定义 init 方法

,

你需要

struct Transition: Decodable {
    let with,from:String
}

然后

let transitions : [Transition]

同时删除

public init(from decoder: Decoder)throws{
    let decoderContainer = try decoder.container(keyedBy: CodingKeys.self)
    states = try decoderContainer.decode([String].self,forKey: .states)
    symbols = try decoderContainer.decode([String].self,forKey: .symbols)
    initialState = try decoderContainer.decode(String.self,forKey: .initialState)
    finalStates = try decoderContainer.decode([String].self,forKey: .finalStates)
    /*
    let nestedContainer = try decoderContainer.nestedContainer(keyedBy: transitionInfoKeys.self,forKey: .transitions)
    with = try nestedContainer.decode([String].self,forKey: .with)
    to = try nestedContainer.decode([String].self,forKey: .to)
    from = try nestedContainer.decode([String].self,forKey: .from)
     */
}

因为这会自动发生,无需手动编写

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。