如何发送嵌套对象作为参数?

Posted

技术标签:

【中文标题】如何发送嵌套对象作为参数?【英文标题】:how to send a nested object as parameter? 【发布时间】:2017-08-09 07:15:35 【问题描述】:

我有一个 json 对象要发送。我应该如何将其作为发布请求快速发送。我不介意使用 alamofire 或本地 http post。

我的对象如下:

  
   "StartAddress":"Colombo",
   "EndAddress":"Kandy",
   "DepartureAddress":"Kollupitiya, Colombo",
   "ArrivalAddress":"Peradeniya, Kandy",
   "CreatedDate":"2017-07-30",
   "Date":"2017-07-30",
   "Time":"2017-07-30",
   "IsLadiesOnly":true,
   "IpAddress":"fe80::8638:38ff:fec8:ea50%wlan0",
   "Country":"Srilanka",
   "VehicleId":"1129",
   "StartLocation":[  
      6.9270974,
      79.8607731
   ],
   "EndLocation":[  
      7.2916216,
      80.6341326
   ],
   "Points":"kgi@ylf",
   "Route":  
      "Bounds":  
         "NorthEast":[  
            7.2916216,
            80.6341326
         ],
         "SouthWest":[  
            6.9270974,
            79.8607731
         ]
      ,
      "Legs":[  
           
            "LegId":1,
            "Distance":14904,
            "Duration":1941,
            "StartAddress":"Colombo",
            "EndAddress":"Kadawatha",
            "StartLocation":[  
               6.9270974,
               79.8612478
            ],
            "EndLocation":[  
               7.0011125,
               79.95000750000001
            ],
            "Ancestors":[  

            ],
            "Price":745
         ,
           
            "LegId":2,
            "Distance":63040,
            "Duration":6209,
            "StartAddress":"Kadawatha",
            "EndAddress":"Kegalle",
            "StartLocation":[  
               7.0011125,
               79.95000750000001
            ],
            "EndLocation":[  
               7.251436200000001,
               80.3466076
            ],
            "Ancestors":[  
               "Colombo"
            ],
            "Price":3152
         ,
           
            "LegId":3,
            "Distance":38990,
            "Duration":4430,
            "StartAddress":"Kegalle",
            "EndAddress":"Kandy",
            "StartLocation":[  
               7.251436200000001,
               80.3466076
            ],
            "EndLocation":[  
               7.2901864,
               80.6338425
            ],
            "Ancestors":[  
               "Colombo",
               "Kadawatha"
            ],
            "Price":1950
         
      ]
   ,
   "TotalPrice":"5847.0",
   "SeatCount":1,
   "Detour":1,
   "Luggage":2,
   "DetoursDescription":"10 Minutes",
   "LuggageDescription":"Small Luggage",
   "Notes":"new ride"

我也尝试过使用 alamofire,但为此我必须将其转换为字典。 我想将此对象作为正文参数发送,我该怎么做?

【问题讨论】:

你想发送 json 对象,对吗?为此,您必须先创建 json 字符串。然后发送一个参数 【参考方案1】:

您应该尝试以下方法:

let parameters: [String: AnyObject] = [
"IdQuiz" : 102,
"IdUser" : "iosclient",
"User" : "iosclient",
"List": [
    [
        "IdQuestion" : 5,
        "IdProposition": 2,
        "Time" : 32
    ],
    [
        "IdQuestion" : 4,
        "IdProposition": 3,
        "Time" : 9
    ]
]
]

 var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")




request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

Alamofire.request(request)
.responseJSON  response in
    // do whatever you want here
    switch response.result 
    case .failure(let error):
        print(error)

        if let data = response.data, let responseString = String(data: data, encoding: .utf8) 
            print(responseString)
        
    case .success(let responseObject):
        print(responseObject)
    

【讨论】:

你使用的是旧的 Alamofire 版本 @zombie 立即查看 如果我想在 URL 中而不是正文中发送参数怎么办?【参考方案2】:

以下是上述 json 对象的示例代码:

do 
        let arrayStartLocation = [7.2916216, 80.6341326]
        let arrayEndLocation = [7.2916216, 80.6341326]
        let arrayNorthEast = [7.2916216, 80.6341326]
        let arraySouthWest = [7.2916216, 80.6341326]
        let dictBounds = ["NorthEast" : arrayNorthEast , "SouthWest": arraySouthWest]
        let dictRoute = ["Bounds" : dictBounds]
        let dictMain: [String : Any] = ["StartAddress": "Colombo",
                        "EndAddress": "Kandy",
                        "DepartureAddress": "Kollupitiya, Colombo",
                        "StartLocation": arrayStartLocation,
                        "EndLocation": arrayEndLocation,
                        "Route": dictRoute

        ///Similarly For remaining keys
        //    .......
        ]


        //Convert to Data
        let jsonData = try JSONSerialization.data(withJSONObject: dictMain, options: JSONSerialization.WritingOptions.prettyPrinted)

        //Convert back to string. Usually only do this for debugging

        if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) 
            print(JSONString)/// Send this string in almofire

            Alamofire.request(AppUrl.CALL_ADD_REVIEWS, method: .post , parameters: ["yourPramName": JSONString]).responseJSON(completionHandler:  (response) in
                //Code here
            )
        
     catch 
        print(error.localizedDescription)
    

【讨论】:

以上是关于如何发送嵌套对象作为参数?的主要内容,如果未能解决你的问题,请参考以下文章

在 Axios 中的 GET 方法 URL 搜索参数中发送嵌套对象

如何在 Javascript 中将 Django 对象作为参数发送?

如何使用 Alamofire 将单个对象作为参数发送

使用 Alamofire 将对象作为参数发送

如何使用Alamofire将单个对象作为参数发送

如何在oracle中将对象类型的对象作为参数传递