2017-08-09 143 views
1

我有一个json对象发送。我应该如何以迅速发送邮件的方式发送邮件。使用alamofire或本机http后,我不介意。如何发送嵌套对象作为参数?

我的目标是象下面这样:

{ 
    "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}[email protected]{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也但是,我必须将其转换为一个字典。 我想发送这个对象作为身体参数,我该怎么做?

+0

要发送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) 
    } 
} 
+0

您使用的是旧的Alamofire版本 – zombie

+0

@ zombie现在检查 –

+0

@ zombie现在在为您工作? –

1

这里是上面的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) 
    }