2017-08-09 144 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) 
    }