【问题标题】:How to send a POST request with nested Dictionary in swift如何在 swift 中使用嵌套字典发送 POST 请求
【发布时间】:2019-03-27 06:44:02
【问题描述】:

我在 StackOverflow 上搜索了 100 多个帖子,但找不到任何正确答案,谁能帮我解开这个谜。

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
    ]
]

使用此代码,只有第一个模型被发送到服务器,第二个模型通过空白值传递

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{    } }

请帮帮我。

【问题讨论】:

  • "只有第一个模型被发送到服务器,第二个模型通过空白值"第二个模型是什么?你能具体说明一下吗?
  • "CustomerTicket" 是类似于嵌套 json 的第二种模型。 [“RequestID”:requestID,“TaskID”:taskID,“Description”:cmets,“TicketTypeID”:3,“CustomerID”:customerID,“PriorityID”:3,“CustomerTPPID”:TTPIDArray]

标签: ios json swift alamofire


【解决方案1】:

你可以这样使用

    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{}}

【讨论】:

  • 显示此错误 { "Error" : true, "Message" : "Object reference not set to an instance of an object." }
  • 你设置了标题
  • 是的,我发送 header let userToken: HTTPHeaders = [ "Authorization": "bearer (accessToken)", "Content-Type": "application/x-www-form-urlencoded" ]
  • 在您提供的链接上显示此错误 { "Error" : true, "Message" : "Object reference not set to an instance of an object." }
  • 在尝试更新代码后,它显示成功消息,但票证 id = 0,即发布了问题反馈参数,但未发布自定义票证参数。 { “错误”:假​​,“TicketID”:0,“FeedBackID”:237119,“消息”:“成功”}
【解决方案2】:

谢谢大家, 我解决了这个问题。 这是此问题的分步解决方案。 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2013-05-14
    • 2017-05-19
    • 2017-10-09
    相关资源
    最近更新 更多