Swift 4 可编码解码json
Swift 4 Codable decoding json
我正在尝试实施新的 Codable
协议,因此我将 Codable
添加到我的结构中,但 无法解码 JSON.
这是我之前的:
结构-
struct Question {
var title: String
var answer: Int
var question: Int
}
客户-
...
guard let data = data else {
return
}
do {
self.jsonResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
let questionItems = self.jsonResponse?["themes"] as! [[String: Any]]
questionItems.forEach {
let item = Question(title: [=13=]["title"] as! String,
answer: [=13=]["answer"] as! Int,
question: [=13=]["question"] as! Int)
questionData.append(item)
}
} catch {
print("error")
}
这就是我现在所拥有的,只是我无法弄清楚解码器部分:
结构 -
struct Question: Codable {
var title: String
var answer: Int
var question: Int
}
客户 -
...
let decoder = JSONDecoder()
if let questions = try? decoder.decode([Question].self, from: data) {
// Can't get past this part
} else {
print("Not working")
}
它打印 "Not working" 因为我无法通过 decoder.decode
部分。有任何想法吗? post 是否需要任何额外的代码,谢谢!
编辑:
API JSON 样本:
{
"themes": [
{
"answer": 1,
"question": 44438222,
"title": "How many letters are in the alphabet?"
},
{
"answer": 0,
"question": 44438489,
"title": "This is a random question"
}
]
}
如果我打印 self.jsonResponse
我得到这个:
Optional(["themes": <__NSArrayI 0x6180002478f0>(
{
"answer" = 7;
"question" = 7674790;
title = "This is the title of the question";
},
{
"answer_" = 2;
"question" = 23915741;
title = "This is the title of the question";
}
我的新密码:
struct Theme: Codable {
var themes : [Question]
}
struct Question: Codable {
var title: String
var answer: Int
var question: Int
}
...
if let decoded = try? JSONDecoder().decode(Theme.self, from: data) {
print("decoded:", decoded)
} else {
print("Not working")
}
您收到此错误是因为您的 JSON 的结构可能如下:
{
"themes": [
{ "title": ..., "question": ..., "answer": ... },
{ "title": ..., "question": ..., "answer": ... },
{ ... }
],
...
}
但是,您编写的代码需要在顶层有一个 [Question]
。您需要的是具有 themes
属性 的不同顶级类型,即 [Question]
。当您解码该顶级类型时,您的 [Question]
将被解码为 themes
键。
如果你的JSON有一个结构
{"themes" : [{"title": "Foo", "answer": 1, "question": 2},
{"title": "Bar", "answer": 3, "question": 4}]}
您需要 themes
对象的等价物。添加这个结构
struct Theme : Codable {
var themes : [Question]
}
现在你可以解码 JSON:
if let decoded = try? JSONDecoder().decode(Theme.self, from: data) {
print("decoded:", decoded)
} else {
print("Not working")
}
包含 Question
个对象被隐式解码。
你好@all 我已经添加了 JSON 编码和 Swift 解码的代码 4.
请使用linkhere
我正在尝试实施新的 Codable
协议,因此我将 Codable
添加到我的结构中,但 无法解码 JSON.
这是我之前的:
结构-
struct Question {
var title: String
var answer: Int
var question: Int
}
客户-
...
guard let data = data else {
return
}
do {
self.jsonResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
let questionItems = self.jsonResponse?["themes"] as! [[String: Any]]
questionItems.forEach {
let item = Question(title: [=13=]["title"] as! String,
answer: [=13=]["answer"] as! Int,
question: [=13=]["question"] as! Int)
questionData.append(item)
}
} catch {
print("error")
}
这就是我现在所拥有的,只是我无法弄清楚解码器部分:
结构 -
struct Question: Codable {
var title: String
var answer: Int
var question: Int
}
客户 -
...
let decoder = JSONDecoder()
if let questions = try? decoder.decode([Question].self, from: data) {
// Can't get past this part
} else {
print("Not working")
}
它打印 "Not working" 因为我无法通过 decoder.decode
部分。有任何想法吗? post 是否需要任何额外的代码,谢谢!
编辑:
API JSON 样本:
{
"themes": [
{
"answer": 1,
"question": 44438222,
"title": "How many letters are in the alphabet?"
},
{
"answer": 0,
"question": 44438489,
"title": "This is a random question"
}
]
}
如果我打印 self.jsonResponse
我得到这个:
Optional(["themes": <__NSArrayI 0x6180002478f0>(
{
"answer" = 7;
"question" = 7674790;
title = "This is the title of the question";
},
{
"answer_" = 2;
"question" = 23915741;
title = "This is the title of the question";
}
我的新密码:
struct Theme: Codable {
var themes : [Question]
}
struct Question: Codable {
var title: String
var answer: Int
var question: Int
}
...
if let decoded = try? JSONDecoder().decode(Theme.self, from: data) {
print("decoded:", decoded)
} else {
print("Not working")
}
您收到此错误是因为您的 JSON 的结构可能如下:
{
"themes": [
{ "title": ..., "question": ..., "answer": ... },
{ "title": ..., "question": ..., "answer": ... },
{ ... }
],
...
}
但是,您编写的代码需要在顶层有一个 [Question]
。您需要的是具有 themes
属性 的不同顶级类型,即 [Question]
。当您解码该顶级类型时,您的 [Question]
将被解码为 themes
键。
如果你的JSON有一个结构
{"themes" : [{"title": "Foo", "answer": 1, "question": 2},
{"title": "Bar", "answer": 3, "question": 4}]}
您需要 themes
对象的等价物。添加这个结构
struct Theme : Codable {
var themes : [Question]
}
现在你可以解码 JSON:
if let decoded = try? JSONDecoder().decode(Theme.self, from: data) {
print("decoded:", decoded)
} else {
print("Not working")
}
包含 Question
个对象被隐式解码。
你好@all 我已经添加了 JSON 编码和 Swift 解码的代码 4.
请使用linkhere