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

标签 ios swift alamofire

我有一个 json 对象要发送。我应该如何快速将其作为帖子请求发送。我不介意使用 alamofire 或 native 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":"k}gi@y{lf",
   "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,但为此我必须将其转换为字典。 我想将此对象作为正文参数发送,我该怎么做?

最佳答案

您应该尝试以下操作:

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

关于ios - 如何将嵌套对象作为参数发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45583963/

相关文章:

ios - 如何根据 JSON 数组中的字段对表格 View 单元格进行分组

php - 将图像文件获取到 body 参数

ios - 如何在 Alamofire 中使用 header 中的 Token 和基本身份验证发出请求?

objective-c - UIWebView 模拟器突然不工作了

ios - 为什么 Apple 翻转了 UIBezierPath 的单位圆?

ios - 如何在 objective-c 中定义一个函数

ios - 如何在 tableview 单元格之间将数据从 firebase 传递到 View Controller ?

ios - 在 Storyboard中更改 UIImageView 的大小

ios - Alamofire 2.0 和 Swift 2 - 标题不工作。查看如何修复它

javascript - 需要播放/停止按钮 javascript 帮助