用swift中的字符串列表构造JSON负载
我会为一个售后服务调用动态地构造下面的有效负载。我不知道如何在字典中创建字符串列表用swift中的字符串列表构造JSON负载,json,swift,Json,Swift,我会为一个售后服务调用动态地构造下面的有效负载。我不知道如何在字典中创建字符串列表 { "Id":"2025", "deviceType": "iphone", "category": ["Dental", "Vision"] } 我的代码 var categoryList = ["Dental", "Vision"] let json = ["Id": "2025", "deviceType": "iphone", "category": categoryList]
{
"Id":"2025",
"deviceType": "iphone",
"category": ["Dental", "Vision"]
}
我的代码
var categoryList = ["Dental", "Vision"]
let json = ["Id": "2025", "deviceType": "iphone", "category": categoryList] as! Dictionary<String, String>
let jsonData = try? JSONSerialization.data(withJSONObject: json)
您可以这样做:-
无需将json转换为数据,您可以直接将此json作为参数发布到服务调用中
let jsonToSend:[String:Any] = [
"Id":"pass your id here",
"deviceType":"device type here",
"category":"category array here"
]
检查这个答案也许可以帮助你改变!字典改为[字符串:任意]。