Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用swift中的字符串列表构造JSON负载_Json_Swift - Fatal编程技术网

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

检查这个答案也许可以帮助你改变!字典改为[字符串:任意]。