【发布时间】:2020-03-18 19:32:38
【问题描述】:
我需要用大写的首字母(又名 PascalCase 或 UppperCamelCase)解码 JSON,如下所示:
{
"Title": "example",
"Items": [
"hello",
"world"
]
}
于是我创建了一个符合Codable的模型:
struct Model: Codable {
let title: String
let items: [String]
}
但JSONDecoder 引发错误,因为大小写不同。
Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "title", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\").", underlyingError: nil))
我想将模型的属性保留在 camelCase 中,但我无法更改 JSON 格式。
【问题讨论】:
标签: json swift uppercase codable decodable