let jsonData = jsonString.data(using: .utf8)! let decoder = JSONDecoder() let jsonModel = try! decoder.decode(H5Editor.self, from: jsonData)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
openfuncdecode<T : Decodable>(_ type: T.Type, from data: Data)throws -> T { let topLevel: Any do { topLevel = tryJSONSerialization.jsonObject(with: data) } catch { throwDecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: error)) } // JSONDecoder 的初始化 let decoder = __JSONDecoder(referencing: topLevel, options: self.options) // 从顶层开始解析 JSON guardlet value = try decoder.unbox(topLevel, as: type) else { throwDecodingError.valueNotFound(type, DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value.")) }