我需要使用嵌套对象创建答案JSON
我尝试使用mappable,但无法解决问题
我需要创建这种数组
{
"interactionId":"daade6b6adcd8b063a355e28bc1f1341",
"futureSurveyAnswers":[
{
"type":"imagepicker",
"qcode":"vxo20zeezo",
"values":[
{
"value":"lion"
}
]
},
{
"type":"radiogroup",
"qcode":"s4ep4s0shf",
"values":[
{
"value":"item1"
}
]
},
{
"type":"checkbox",
"qcode":"76k5cpnpki",
"values":[
{
"value":"item1"
},
{
"value":"item2"
}
]
}
],
"originalResultArray":"{\"question2\":\"item1\",\"question3\":[\"item1\",\"item2\"],\"question1\":\"lion\"}"
}
答案 0 :(得分:2)
当前最好使用的最佳解决方案是Codable
协议。在线上有很多示例和教程,因此请检查一两个,我相信您会发现它非常简单。
关键是您可以直接在JSON中编码具体的类或结构。因此,您的问题就不在JSON本身,而只是结构化您的类。例如:
class Survey {
class Answer {
enum AnswerType: String {
case imagepicker
}
class Value {
var value: String?
}
var type: AnswerType = .imagepicker
var qcode: String?
var values: [Value] = [Value]()
}
let interactionId: String
var futureSurveyAnswers: [Answer] = [Answer]()
var originalResultArray: String?
init(interactionId: String) { self.interactionId = interactionId }
}
这样,您可以轻松地根据需要修改结构。例如:
let survey = Survey(interactionId: "0")
survey.futureSurveyAnswers.append({
let answer = Survey.Answer()
answer.qcode = "test"
return answer
}())
因此,要将其扩展到可编码,您实际上需要将Codable
协议附加到您的类和任何嵌入式组件上:
class Survey: Codable {
class Answer: Codable {
enum AnswerType: String, Codable {
case imagepicker
}
class Value: Codable {
var value: String?
}
var type: AnswerType = .imagepicker
var qcode: String?
var values: [Value] = [Value]()
}
let interactionId: String
var futureSurveyAnswers: [Answer] = [Answer]()
var originalResultArray: String?
init(interactionId: String) { self.interactionId = interactionId }
}
现在您可以像以下这样简单地获取JSON:
let survey = Survey(interactionId: "0")
survey.futureSurveyAnswers.append({
let answer = Survey.Answer()
answer.values.append({
let value = Survey.Answer.Value()
value.value = "some"
return value
}())
answer.qcode = "test"
return answer
}())
let encoder = JSONEncoder()
if let jsonData = try? encoder.encode(survey) {
print("Generated data: \(jsonData)")
}
我希望这能为您指明正确的方向。
在评论中,这就是我用作JSON的测试示例:
class Survey: Codable {
class Answer: Codable {
enum AnswerType: String, Codable {
case imagepicker
}
class Value: Codable {
var value: String?
init(value: String? = nil) { self.value = value }
}
var type: AnswerType = .imagepicker
var qcode: String?
var values: [Value] = [Value]()
}
let interactionId: String
var futureSurveyAnswers: [Answer] = [Answer]()
var originalResultArray: String?
init(interactionId: String) { self.interactionId = interactionId }
}
func addTestAnswer(to survey: Survey) {
let answer = Survey.Answer()
answer.values.append(.init(value: "Random value \(Int.random(in: 1...100))"))
answer.values.append(.init(value: "Random value \(Int.random(in: 1...100))"))
answer.values.append(.init(value: "Random value \(Int.random(in: 1...100))"))
answer.values.append(.init(value: "Random value \(Int.random(in: 1...100))"))
survey.futureSurveyAnswers.append(answer)
}
func testRandomSurveyJSON() {
let survey = Survey(interactionId: "randomSurvey")
addTestAnswer(to: survey)
addTestAnswer(to: survey)
addTestAnswer(to: survey)
addTestAnswer(to: survey)
addTestAnswer(to: survey)
print("Got JSON: \(String(data: try! JSONEncoder().encode(survey), encoding: .utf8)!)")
}
并得到以下结果:
Got JSON: {"interactionId":"randomSurvey","futureSurveyAnswers":[{"type":"imagepicker","values":[{"value":"Random value 74"},{"value":"Random value 4"},{"value":"Random value 26"},{"value":"Random value 93"}]},{"type":"imagepicker","values":[{"value":"Random value 43"},{"value":"Random value 65"},{"value":"Random value 38"},{"value":"Random value 88"}]},{"type":"imagepicker","values":[{"value":"Random value 56"},{"value":"Random value 88"},{"value":"Random value 57"},{"value":"Random value 94"}]},{"type":"imagepicker","values":[{"value":"Random value 66"},{"value":"Random value 52"},{"value":"Random value 89"},{"value":"Random value 27"}]},{"type":"imagepicker","values":[{"value":"Random value 53"},{"value":"Random value 93"},{"value":"Random value 30"},{"value":"Random value 55"}]}]}
这似乎是正确的。