2017-02-13 96 views
-6

我想創建一個json字符串並將其發送到服務器。我想發送我提到的JSON和Alamofire庫。現在的問題是如何在快速編程中創建這個json字符串?如何在swift中創建json字符串並將其發送到服務器

{"Notification": 
[ 
{"id":"15","TableName":"HadafShakhsi","RowId":"148424-1","Operation":"2","StoreName":"SelectHadafShakhsi","isFirst":"1"} 
] 
} 
+0

你能請張貼,你還沒有嘗試過?請提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – FelixSFD

+0

NSJONSerialization?你有什麼嘗試? – Larme

+1

NSJONSerialization?謝謝 –

回答

3
let parameters: [String: AnyObject] = [ 
    "Notification": [ 

    "id": "15", 
    .......... 
    ...... 


] 
    ] 

Alamofire.request(.POST, "http://server.com", parameters: parameters, encoding: .JSON) 
    .responseJSON { request, response, JSON, error in 
     print(response) 
     print(JSON) 
     print(error) 
    } 
0

你可以做一個函數prepareforObject作爲

func prepareObjects(dict : Dictionary<String, Any>) -> [String: AnyObject] { 

     var dictParameters = [String: AnyObject]() 
     for (key, value) in dict { 
      dictParameters[key] = "\(value)" as AnyObject? 
     } 

     return dictParameters 
    } 

,然後調用Alamofire作爲

Alamofire.request(.POST, "http://server.com", parameters: prepareforObject(parameters), encoding: .JSON) 
    .responseJSON { request, response, JSON, error in 
     print(response) 
     print(JSON) 
     print(error) 
    } 
相關問題