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

问题描述 投票:0回答:1

这是我的api请求:

{
"billDetails": {
           "billerId":"EPDCLOB00ANP01",
            "customerParams":[{"name":"Service Number","value":"116515M025007621"}]
             }
}

这里是代码:

func billerFetchService(){

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


let url = URL(string: "https://app.anyemi.com/anyemi_v1/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错误中添加这样的内容

一行上的连续语句必须用';'分隔插入 ';'期望的表达式

我做错了,请在代码中帮助我

ios json swift post parameter-passing
1个回答
0
投票

请以此为参数

 let parameters = ["billDetails":
         [
          "billerId": "EPDCLOB00ANP01",
         "customerParams" : ["name":"Service Number","value":"116515M025007621"]
        ]
        ] as [String : Any]
© www.soinside.com 2019 - 2024. All rights reserved.