gpt4 book ai didi

json - 如何使用 Swift 4.2 解决 JSON 可解码错误异常?

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:58 28 4
gpt4 key购买 nike

我的场景是,我试图从 JSON 获取 data 并加载到 tableView 中。在 compile 时间里,我从 JSON decodable 进程中获取了 badexception。在我使用的 JSON 响应下方,我无法获得确切的问题。请提供一些建议

我的 JSON 响应

{
"status": true,
"status_code": 1,
"data": [
{
"cat_id": "1",
"cat_name": "Food",
"cat_parentid": "2",
"name": "guru",
"year": "3000",
"fullname": {
"firstname": "jio",
"lastname": "jack"
},
"address": {
"city": "sanfrancisco",
"state": "california"
},
"ship": {
"captian": "mojo",
"time": "12.30.01"
}
}
]
}

我的可解码

struct Root: Decodable {
let catID, catName, catParentid, name, year: String?

private enum CodingKeys: String, CodingKey {
case catID = "cat_id"
case catName = "cat_name"
case catParentid = "cat_parentid"
case name, year
}
}

我的 JSON 代码

func parseJSON() {

let url = URL(string: "https://...")

let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in

guard error == nil else {
print("returning error")
return
}

guard let content = data else {
print("not returning data")
return
}

guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
print("Error")
return
}

do {
let content = json["data"] as! [[String:String]]
let staData = try JSONSerialization.data(withJSONObject:content,options:[])
self.tableArray = try JSONDecoder().decode([Root].self, from:staData)
}
catch {
print(error)
}

print(self.tableArray)

DispatchQueue.main.async {
self.tableView.reloadData()
}

}
task.resume()
}

我的问题

issue screen

最佳答案

我已经从 HERE 生成了 Codable 协议(protocol)它看起来像:

struct Root: Codable {
let status: Bool
let statusCode: Int
let data: [Datum]

enum CodingKeys: String, CodingKey {
case status
case statusCode = "status_code"
case data
}
}

struct Datum: Codable {
let catID, catName, catParentid, name: String
let year: String
let fullname: Fullname
let address: Address
let ship: Ship

enum CodingKeys: String, CodingKey {
case catID = "cat_id"
case catName = "cat_name"
case catParentid = "cat_parentid"
case name, year, fullname, address, ship
}
}

struct Address: Codable {
let city, state: String
}

struct Fullname: Codable {
let firstname, lastname: String
}

struct Ship: Codable {
let captian, time: String
}

现在我已经在 Playground 上用你的 JSON 检查了它,如下所示:

let jsonData = """
{
"status": true,
"status_code": 1,
"data": [
{
"cat_id": "1",
"cat_name": "Food",
"cat_parentid": "2",
"name": "guru",
"year": "3000",
"fullname": {
"firstname": "jio",
"lastname": "jack"
},
"address": {
"city": "sanfrancisco",
"state": "california"
},
"ship": {
"captian": "mojo",
"time": "12.30.01"
}
}
]
}
""".data(using: .utf8)!

你可以这样解析它:

let root = try? JSONDecoder().decode(Root.self, from: jsonData)

现在您可以使用

root 对象获取数据数组
let content = root?.data //here your crash will resolve

content 是一种 [Datum]

关于json - 如何使用 Swift 4.2 解决 JSON 可解码错误异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726739/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com