如何使用alamofire发送此参数

时间:2019-01-01 20:15:16

标签: swift alamofire

我想使用 Alamofire 发送这些参数,但是问题出在参数内部,有一个名为String的{​​{1}}数组。如何将其转换为person才能发送?

以下是要发送的参数:

Dictionary

到目前为止我所拥有的:

{
  "BRANCH_ID" : 23,
  "PACKAGE_ID": 10,
  "ARRIVE_TIME_ID": 7,
  "ARRIVE_DATE" : "12/2/2014",
  "IS_TABLE" : true,
  "IS_TICKET" : false,
  "NOTE": "hi",
  "persons": "[{
    \"ID\":\" 1\",
    \"FIRST_NAME\":\"Ali\",
    \"LAST_NAME\":\"Ahmed\",
    \"GENDER\":\"Male\",
    \"EMAIL\":\"mz_@.fgj\",
    \"FACEBOOK_URL\":\"www.google.com\",
    \"IS_MEMBER\":\"true\",
    \"IS_BOOKER\":\"true\"  
  }]",
  "addons": "[]"
}

2 个答案:

答案 0 :(得分:0)

似乎问题是persons参数是字典数组(每个字典代表一个人);基于您在代码中实现的paramters

let paramters : [String : Any ] = [
    "BRANCH_ID" : "23",
    "PACKAGE_ID": "10",
    "ARRIVE_TIME_ID": "12",
    "ARRIVE_DATE" : "12/2/2014",
    "IS_TABLE" : true,
    "IS_TICKET" : false ,
    "NOTE": "",
    "persons" : [ "ID": "1",
                  "FIRST_NAME":"Ali",
                  "LAST_NAME":"Ahmed",
                  "GENDER":"Male",
                  "MOBILE":"0111281456",
                  "EMAIL":"mz_@.fgj",
                  "FACEBOOK_URL":"www.google.com",
                  "IS_MEMBER": true,
                  "IS_BOOKER": true] ,
    "addons": []
]

您正在将persons实现为单个字典,而不是字典数组。

解决方法是将person声明为包含单个字典的数组,例如:

let paramters : [String : Any ] = [
    "BRANCH_ID" : "23",
    "PACKAGE_ID": "10",
    "ARRIVE_TIME_ID": "12",
    "ARRIVE_DATE" : "12/2/2014",
    "IS_TABLE" : true,
    "IS_TICKET" : false ,
    "NOTE": "",
    "persons" : [[ "ID": "1",
                  "FIRST_NAME":"Ali",
                  "LAST_NAME":"Ahmed",
                  "GENDER":"Male",
                  "MOBILE":"0111281456",
                  "EMAIL":"mz_@.fgj",
                  "FACEBOOK_URL":"www.google.com",
                  "IS_MEMBER": true,
                  "IS_BOOKER": true]] ,
    "addons": []
]


旁注:应为“参数”而不是“参数”

答案 1 :(得分:0)

在JSON中,"person"具有字符串值,而不是对象数组,并且"addons"也具有字符串值。我认为这不是您想要的,但是这里是如何在Swift中获取它。

let json: [String: Any] = ["BRANCH_ID": 23, "PACKAGE_ID": 10,
                           "ARRIVE_TIME_ID": 7,
                           "ARRIVE_DATE" : "12/2/2014",
                           "IS_TABLE" : true,
                           "IS_TICKET" : false,
                           "NOTE": "hi",
                           "persons": "[{ " + // <-- Multi-line string not array of objects
                            "\"ID\":\" \"1\", " +
                            "\"FIRST_NAME\":\"Ali\", " +
                            "\"LAST_NAME\":\"Ahmed\", " +
                            "\"GENDER\":\"Male\", " +
                            "\"EMAIL\":\"mz_@.fgj\", " +
                            "\"FACEBOOK_URL\":\"www.google.com\", " +
                            "\"IS_MEMBER\":\"true\", " +
                            "\"IS_BOOKER\":\"true\" " +
                            "}]", // <-- end of Multi-line string
                           "addons": "[]"] // <-- String not array

let string = try! String(data: JSONSerialization.data(withJSONObject: json, options: .prettyPrinted), encoding: .utf8)!
print(string)