Swift 如何使用 Codable 协议解析字符串数组
Swift How to parse string array using Codable Protocol
这是我的 "wrapper" class:
class QuestionResult: Codable {
var title: String?
var questions: [Question]?
}
问题class:
class Question: Codable{
var question_id: Int?
var question_type: String?
var question: String?
var answers: [String]?
}
这是相对的JSON:
{
"title":"sondaggio di test",
"start_message":"<p>sodaggio di prova</p>\r\n",
"end_message":"<p>fine sondaggio di test</p>\r\n",
"start_date":"2020-05-01",
"end_date":"2020-05-22",
"voted":false,
"questions":[
{
"question_id":418,
"question_type":"number",
"question":"domanda test 1"
},
{
"question_id":419,
"question_type":"checkbox",
"question":"domanda test 2",
"answers":[
"risp1",
"risp2",
"risp3",
"risp4",
"risp5"
]
}
]
}
现在,除了 return 为 nil 的 "answers" 属性外,所有属性都已正确解析。
如何使用 Codable 协议解析字符串数组?
这是模型:
struct QuestionResult: Codable {
let title, startMessage, endMessage, startDate: String
let endDate: String
let voted: Bool
let questions: [Question]
enum CodingKeys: String, CodingKey {
case title
case startMessage = "start_message"
case endMessage = "end_message"
case startDate = "start_date"
case endDate = "end_date"
case voted, questions
}
}
struct Question: Codable {
let questionID: Int
let questionType, question: String
let answers: [String]?
enum CodingKeys: String, CodingKey {
case questionID = "question_id"
case questionType = "question_type"
case question, answers
}
}
无论如何,我认为您的模型没有错。但是请尝试此代码以确保:
let result = try? JSONDecoder().decode(QuestionResult.self, from: data)
print(result?.questions[1].answers)
这是我的 "wrapper" class:
class QuestionResult: Codable {
var title: String?
var questions: [Question]?
}
问题class:
class Question: Codable{
var question_id: Int?
var question_type: String?
var question: String?
var answers: [String]?
}
这是相对的JSON:
{
"title":"sondaggio di test",
"start_message":"<p>sodaggio di prova</p>\r\n",
"end_message":"<p>fine sondaggio di test</p>\r\n",
"start_date":"2020-05-01",
"end_date":"2020-05-22",
"voted":false,
"questions":[
{
"question_id":418,
"question_type":"number",
"question":"domanda test 1"
},
{
"question_id":419,
"question_type":"checkbox",
"question":"domanda test 2",
"answers":[
"risp1",
"risp2",
"risp3",
"risp4",
"risp5"
]
}
]
}
现在,除了 return 为 nil 的 "answers" 属性外,所有属性都已正确解析。 如何使用 Codable 协议解析字符串数组?
这是模型:
struct QuestionResult: Codable {
let title, startMessage, endMessage, startDate: String
let endDate: String
let voted: Bool
let questions: [Question]
enum CodingKeys: String, CodingKey {
case title
case startMessage = "start_message"
case endMessage = "end_message"
case startDate = "start_date"
case endDate = "end_date"
case voted, questions
}
}
struct Question: Codable {
let questionID: Int
let questionType, question: String
let answers: [String]?
enum CodingKeys: String, CodingKey {
case questionID = "question_id"
case questionType = "question_type"
case question, answers
}
}
无论如何,我认为您的模型没有错。但是请尝试此代码以确保:
let result = try? JSONDecoder().decode(QuestionResult.self, from: data)
print(result?.questions[1].answers)