【发布时间】:2021-03-22 11:30:35
【问题描述】:
我有这样的 JSON:
{
"states" : [
"C",
"A",
"B",
"Sink",
],
"symbols" : [
"c",
"a",
"b"
],
"transitions" : [
{
"with" : "c",
"to" : "B",
"from" : "C"
},
{
"with" : "c",
"to" : "Sink",
"from" : "C"
},
{
"with" : "b",
"to" : "B",
"from" : "B"
},
{
"with" : "b",
"to" : "Sink",
"from" : "B"
},
{
"with" : "c",
"to" : "C",
"from" : "A"
},
{
"with" : "c",
"to" : "Sink",
"from" : "A"
},
{
"with" : "a",
"to" : "A",
"from" : "A"
},
{
"with" : "a",
"to" : "Sink",
"from" : "A"
}
],
"initialState" : "A",
"finalStates" : [
"B"
]
}
我无法解码 JSON 中的 transitions 部分(我需要像苹果在 here 中那样解码它)。
到目前为止我得到的是这个(注释部分是领先的错误 typeMismatch(Swift.Dictionary
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)
*/
}
}
【问题讨论】:
-
不清楚你到底想要什么。为什么要展平过渡?为什么注意有 struct
Transition: Codable { let with: String; let to: String; let from: String }和FiniteAutomata:let transitions: [Transition]?let with:[String]不是一个好主意,因为您将失去与from和to的同步。
标签: json swift codable decodable