ios - 如何在快速解析时在参数中添加post api请求

标签 ios json swift post parameter-passing

这是我的 api 请求:

{
"billDetails": {
           "billerId":"EPDCLOB00AN232",
            "customerParams":[{"name":"Service Number","value":"116515M025033"}]
             }
}

代码如下:

func billerFetchService(){

let parameters = ["billDetails": {
    "billerId" : "EPDCLOB00ANP01",
    "customerParams" : [{"name":"Service Number","value":"116515M025007621"}]
                                 }
                  ] as [String : Any]


let url = URL(string: "https://app.com/Fetch/fetch")
var req =  URLRequest(url: url!)
req.httpMethod = "POST"
req.addValue("application/json", forHTTPHeaderField: "Contet-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")

guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
    if response != nil {
        // print(response)
    }
    if let data = data {
        do{
            var json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
            print("fetching json \(json)")

        }catch{
            print("error")
        }
    }
}).resume()
}

如果我在parameters中这样添加错误

Consecutive statements on a line must be separated by ';'
Insert ';' Expected expression

哪里写错了,请帮我写代码

最佳答案

你需要

let parameters = ["billDetails": [
    "billerId" : "EPDCLOB00ANP01",
    "customerParams" : [["name":"Service Number","value":"116515M025007621"]]]]

关于ios - 如何在快速解析时在参数中添加post api请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929817/

相关文章:

ios - ImageView 的奇怪定位。错误的内容模式?

android - 在 Firemonkey 中获取 Android 设备和操作系统信息

c# - JsonConvert.DeserializeObject 特殊字符 未终止的字符串。预期的分隔符 :

ios - Swift iOS -DispatchGroup with URLSession 正在锁定它不在的应用程序的其他部分

swift - 我需要一个 UIButton 飞入屏幕

iphone - 应用设计-菜单系统

ios - UITableView 在左右边框上有意想不到的空格

json - 如何将 jq 与变量连接?

C# Web API POST 参数 FromBody 始终为 null

swift - 尝试连接到我的服务器时使用 Alamofire 和 swift 出现 401 错误