我正在尝试将数据从静态JSON文件解析到我的实体,但出现以下错误:Error serializing json typeMismatch
想法是创建一个公式收集应用程序。每次启动应用程序时,都应通过加载JSON来加载公式集合。这样,更容易更新公式列表。
我想JSON解释了预期的数据结构。
我的JSON:
[
{
"category_name": "Wasserbau",
"icon": "wave",
"formula_list": [
{
"formula_name": "Formel 1",
"formula_description": "Demo Formel für Testat",
"formula": "x=y/z",
"favorite": false
},
{
"formula_name": "Formel 2",
"formula_description": "Demo Formel für Testat",
"formula": "x=y/z",
"favorite": false
}
]
},
{
"category_name": "Strassenbau",
"icon": "wave",
"formula_list": [
{
"formula_name": "Formel 3",
"formula_description": "Demo Formel für Testat",
"formula": "x=y/z",
"favorite": false
},
{
"formula_name": "Formel 4",
"formula_description": "Demo Formel für Testat",
"formula": "x=y/z",
"favorite": false
}
]
},
{
"category_name": "Hochbau",
"icon": "wave",
"formula_list": [
{
"formula_name": "Formel 5",
"formula_description": "Demo Formel für Testat",
"formula": "x=y/z",
"favorite": false
},
{
"formula_name": "Formel 6",
"formula_description": "Demo Formel für Testat",
"formula": "x=y/z",
"favorite": false
}
]
}
]
我尝试解析JSON的函数:
func loadCategoryJSONData() {
let jsonUrlString = "https://joshuahemmings.ch/data.json"
guard let url = URL(string: jsonUrlString) else {return} //
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
print("JSON Response: ")
print(data)
do {
let categoryJSONData = try JSONDecoder().decode(CategoryList.self, from: data)
for category in categoryJSONData.data {
print(category.categoryName)
}
} catch let jsonErr {
print("Error serializing json", jsonErr)
}
}.resume()
}
还有我的实体:
struct CategoryList: Decodable {
let data: [Category]
}
struct Formula: Codable {
let formulaName : String
let formulaDescription : String
let formula : String
let favorite: Bool
enum CodingKeys: String, CodingKey {
case formulaName = "formula_name"
case formulaDescription = "formula_description"
case formula
case favorite
}
init(_ dictionary: [String: Any]) {
self.formulaName = dictionary["formulaName"] as? String ?? ""
self.formulaDescription = dictionary["formulaDescription"] as? String ?? ""
self.formula = dictionary["formula"] as? String ?? ""
self.favorite = dictionary["favorite"] as? Bool ?? false
}
}
struct Category: Codable {
let categoryName: String
let icon: String
let formulaList: [Formula]
enum CodingKeys: String, CodingKey {
case categoryName = "category_name"
case icon = "icon"
case formulaList = "formula_list"
}
init(_ dictionary: [String: Any]) {
self.categoryName = dictionary["categoryName"] as? String ?? ""
self.icon = dictionary["icon"] as? String ?? ""
self.formulaList = dictionary["formulaList"] as? [Formula] ?? []
}
}
答案 0 :(得分:0)
您的主要问题是声明要解析此JSON
的类型。
您这里没有CategoryList
,就像这样:
{
"data": [ … <Category JSON> … ]
}
您在这里只有[Category]
,就像JSON根是Array
([]
)而不是Object
({}
)一样。
所以不是
try decoder.decode(CategoryList.self, from: data)
你应该做
try decoder.decode([Category].self, from: data)
此外,如果您的编码键与JSON中的键几乎相同,但在蛇形情况下则不同,则不应为类型声明自定义CodingKey
。请改用正确的.keyDecodingStrategy。
struct Formula: Codable {
let formulaName: String
let formulaDescription: String
let formula: String
let favorite: Bool
}
struct Category: Codable {
let categoryName: String
let icon: String
let formulaList: [Formula]
}
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let categoryJSONData = try decoder.decode([Category].self, from: data)