gpt4 book ai didi

ios - 如何使用字典构建以下复杂结构?

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

我需要将具有以下结构的数据传递给网络服务。

所需的结构是:

{  
"Name":"abc",
"fatherName":"xyz",
"class":{
"id":"1",
"className":"MPC",
"classDesc":"Maths,Physics,chemistry"
},
"classLeader":{
"id":"123",
"firstName":"def",
"lastName":"ghi",
"emailId":"def.ghi@jkl.com"
},
"classTeachers":[
{
"id":"567894",
"firstName":"xxx",
"lastName":"V",
"emailId":"xxx.V@jkl.com"
}
]
}

我为 classclassLeaderclassTeachers 有单独的字典,如下所示:

class = ["id":"1","className":"MPC","classDesc":"Maths,Physics,chemistry"]
classLeader = ["id":"123","firstName":"def","lastName":"ghi","emailId":"def.ghi@jkl.com"

classTeacher = ["id":"567894","firstName":"xxx","lastName":"v","emailId":"xxx.v@jkl.com]

还有两个字符串namefathername

我尝试使用 [string:AnyObject] 并将我的字典作为 anyobject 传递,但我无法成功..我得到的输出是

{  
"Name":"abc",
"fatherName":"xyz",
"class":[
"id":"1",
"className":"MPC",
"classDesc":"Maths,Physics,chemistry"
],
"classLeader":[
"id":"123",
"firstName":"def",
"lastName":"ghi",
"emailId":"def.ghi@jkl.com"
],
"classTeachers":[
[
"id":"567894",
"firstName":"xxx",
"lastName":"V",
"emailId":"xxx.V@jkl.com"
]
]
}

我不知道如何构建所需的结构。请帮我解决这个问题。

最佳答案

您可能需要JsonSerialize 数据。试试这个。

let clas = ["id":"1","className":"MPC","classDesc":"Maths,Physics,chemistry"]
let classLeader = ["id":"123","firstName":"def","lastName":"ghi","emailId":"def.ghi@jkl.com"]
let classTeacher = ["id":"567894","firstName":"xxx","lastName":"v","emailId":"xxx.v@jkl.com"]

let dict = ["name": "ABC", "fatherName": "XYZ", "class": clas, "classLeader": classLeader, "classTeachers": classTeacher]

let jsonData = try? NSJSONSerialization.dataWithJSONObject(dict, options: .PrettyPrinted)
let jsonString = NSString(data: jsonData!, encoding: NSUTF8StringEncoding)

print(jsonString)

关于ios - 如何使用字典构建以下复杂结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39888330/

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