ios - 将字典数据转换为 HTTP url 格式

标签 ios swift

我正在尝试使用 POST 方法发送字典数据 dataTaskWithRequest() 只能以字符串 URL 格式发送。

我们如何将字典数据转换成 HTTP URL 格式?

这是一个例子:

let parameters = [
    "first": "name",
    "second": ["a", "b"],
    "third": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
] 

转换成

first=name&second[]=a&second[]=b&third[x]=1&third[y]=2&third[z]=3

使用 swift 2.3

最佳答案

您可以使用此代码将字典转换为 json 并设置 HTTPBody

    let jsondata = NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
    request.HTTPBody = jsondata

关于ios - 将字典数据转换为 HTTP url 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41074076/

相关文章:

android - Android中的Sensor.TYPE_LINEAR_ACCELERATION与IOS中的userAcceleration相同

ios - 带有数组值的 swift json 字符串转换为 [UInt8]

swift - 什么样的内存管理适合 UITableViewRowAction 闭包?

swift - 添加约束时出现运行时错误

ios - 我可以使用 TabBarItem 上的自定义事件点击来制作自定义 UITabBarController 吗?

ios - Cordova 3.5 iOS 编译错误

iOS 获取哪个 uielement 调用了 UIKeyboardWillShowNotification

ios - SwiftUI |警告 : Bound preference _ tried to update multiple times per frame. 可能的原因?

ios - 如何将 UIScrollView 的转换应用于 UIView?

arrays - swift 3 : binary operator cannot be applied to operands of type int and 'Int?'