如何在swift中创建json字符串并将其发送到服务器

时间:2017-02-13 11:44:00

标签: json swift

我想创建一个json字符串并将其发送到服务器。我想发送我在Alamofire库中提到的JSON。现在的问题是如何在swift编程中创建这个json字符串?

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

2 个答案:

答案 0 :(得分: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)
    }

答案 1 :(得分: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)
    }