【问题标题】:Construct a JSON payload with list of strings in swift用 swift 中的字符串列表构造一个 JSON 有效负载
【发布时间】:2017-06-27 23:58:33
【问题描述】:

我将为 POST 服务调用动态构建以下有效负载。我不确定如何在字典中创建字符串列表

{
    "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 swift


【解决方案1】:

你可以这样做:-

并且不需要将json转换为数据,您可以直接将此json作为参数发布到SERVICE CAll中

let jsonToSend:[String:Any] = [
    "Id":"pass your id here",
    "deviceType":"device type here",
    "category":"category array here"
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 2017-04-22
    • 2022-09-27
    • 1970-01-01
    • 2019-12-16
    • 2020-06-30
    • 1970-01-01
    相关资源
    最近更新 更多