gpt4 book ai didi

ios - 如何在swift中将json对象一个一个写入json对象

转载 作者:行者123 更新时间:2023-11-28 11:30:25 25 4
gpt4 key购买 nike

我需要用嵌套对象创建一个答案 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\"}"
}

最佳答案

目前最好的解决方案可能是 Codable 协议(protocol)。网上有很多示例和教程,请检查一两个,我相信您会发现它非常简单易用。

重点是您可以将具体的类或结构直接编码为(和来自)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 协议(protocol)附加到您的类和任何嵌入式组件中:

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"}]}]}

这似乎是正确的。

关于ios - 如何在swift中将json对象一个一个写入json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946287/

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