如何快速发送带有嵌套字典的POST请求

时间:2019-03-27 06:44:02

标签: ios json swift alamofire

我在StackOverflow上搜索了数百条帖子,但找不到正确的答案,谁能帮助我解决这个难题。

let param : [String : Any]= [
    "ServiceReqID" : 1,
    "WebUsersID" : customerID,
    "FirstName" : userName,
    "LastName" : "",
    "Company" : self.profileValues.customerCompanyName,
    "City" : self.profileValues.customerCityName,
    "Email" : self.profileValues.customerEmail,
    "ContactNo" : self.profileValues.customerContactNumber,
    "Country" : "Pakistan",
    "PackageChange" : 0,
    "AddressChange" : 0,
    "TelInternetVAS" : 0,
    "Others" : 0,
    "Comments" : comments,
    "CSAFNO" : self.profileValues.customerCSAFNo,
    "SecondaryContactNo" : "",
    "CustomerTicket" : [
        "RequestID" : requestID,
        "TaskID" : taskID,
        "Description" : comments,
        "TicketTypeID" : 3,
        "CustomerID" : customerID,
        "PriorityID" : 3,
        "CustomerTPPID" : TTPIDArray
    ]
]

使用此代码,仅将第一个模型发送到服务器,并使用空白值传递secound模型

func postserviceRequestFeedback (
    url : String,
    parameter : [String : Any],
    tiket : HTTPHeaders
)
{
    Alamofire.request (
        url,
        method : .post,
        parameters : parameter,
        headers : tiket
    )
    .responseJSON { (response) in     if response.result.isSuccess{    } }

请帮助我。

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式使用

    let customerTckt:[String:Any] = ["RequestID" : requestID ,
    "TaskID" : taskID ,
    "Description" : comments ,
    "TicketTypeID" : 3 ,
    "CustomerID" : customerID ,
    "PriorityID" : 3 ,
    "CustomerTPPID" : TTPIDArray ]

    let param : [String : Any] = ["ServiceReqID" : 1 ,
                                  "WebUsersID" : customerID,
                                  "FirstName" : userName,
                                  "LastName" : "" ,
                               "Company":self.profileValues.customerCompanyName ,
                                  "City" : self.profileValues.customerCityName ,
                                  "Email" : self.profileValues.customerEmail ,
                                  "ContactNo" : self.profileValues.customerContactNumber ,
                                  "Country" : "Pakistan" ,
                                  "PackageChange" : 0 ,
                                  "AddressChange" : 0,
                                  "TelInternetVAS" : 0 ,
                                  "Others" : 0 ,
                                  "Comments" : comments ,
                                  "CSAFNO" : self.profileValues.customerCSAFNo,
                                  "SecondaryContactNo" : "" ,
                                  "CustomerTicket" :customerTckt]


func postserviceRequestFeedback(url : String , parameter : [String : Any] , tiket : tiket ){
Alamofire.request(url, method : .post , parameters : parameter , headers : 
HTTPHeaders).responseJSON { (response) in
    if response.result.isSuccess{}}

答案 1 :(得分:0)

感谢所有人, 我解决了这个问题。 这是此问题的逐步解决方案。 1)

 Alamofire.request(url, method : .post , parameters : parameter , encoding : JSONEncoding.default, headers : tiket ).responseJSON { (response) in
        if response.result.isSuccess{
          let responseJoson : JSON = JSON(response.result.value!)
            print(responseJoson)
         }

编码在这里非常重要。不要跳过这个。 第2步)

 func apiCall(){
  let customerTckt:[String:Any] = ["RequestID" : requestID ,
"TaskID" : taskID ,
"Description" : comments ,
"TicketTypeID" : 3 ,
"CustomerID" : customerID ,
"PriorityID" : 3 ,
"CustomerTPPID" : TTPIDArray ]

let param : [String : Any] = ["ServiceReqID" : 1 ,
                              "WebUsersID" : customerID,
                              "FirstName" : userName,
                              "LastName" : "" ,
                           "Company":self.profileValues.customerCompanyName ,
                              "City" : self.profileValues.customerCityName ,
                              "Email" : self.profileValues.customerEmail ,
                              "ContactNo" : self.profileValues.customerContactNumber ,
                              "Country" : "Pakistan" ,
                              "PackageChange" : 0 ,
                              "AddressChange" : 0,
                              "TelInternetVAS" : 0 ,
                              "Others" : 0 ,
                              "Comments" : comments ,
                              "CSAFNO" : self.profileValues.customerCSAFNo,
                              "SecondaryContactNo" : "" ,
                              "CustomerTicket" :customerTckt]
    let userToken: HTTPHeaders = [
        "Authorization": "bearer \(accessToken)",
        "Content-Type": "application/json"
    ]
   postserviceRequestFeedback(url: postRequestFeedbackUrl, parameter: param , tiket: userToken)
   }

不要将“ Content-Type”跳过到application / json