Alamofire 发送对象作为参数

Rad*_*nov 5 swift alamofire swift2

我很难做一些简单的事情。我要发送的数据如下:

  {
    "nickname":"Rado",
    "social": {
      "data: { 
        "accesstoken":"xx",
        "applicationId":"xx",
        "userId":"xx"
      },
      "type":"whatever"
    }
  }
Run Code Online (Sandbox Code Playgroud)

目前我正在这样做:

           let params = [
                "nickname": userName,
                "social": [
                    "type": "whatever",
                    "data": [
                        "userId": accessToken.userID,
                        "accesstoken": accessToken.tokenString,
                        "applicationId": accessToken.appID
                    ]
                ]
            ]

 Alamofire.request(.POST, "url/users", parameters: params, headers: nil)
                .responseJSON { response in
Run Code Online (Sandbox Code Playgroud)

}

作为回应,我得到了这个:

{
 "nickname":"Rado",
 "social[data][userId]":"xx",
 "social[data][applicationId]":"xx",
 "social[data][accesstoken]":"xx",
 "social[type]":"something"
}
Run Code Online (Sandbox Code Playgroud)

任何建议将被认真考虑!

Rad*_*nov 5

事实证明,解决方案非常简单。我缺少编码:.JSON

    Alamofire.request(.POST, "url/users", parameters: params, headers: nil, encoding: .JSON)
        .responseJSON { response in

    }
Run Code Online (Sandbox Code Playgroud)

  • 同样的问题在这里。JSONEncoding.default 是我的解决方案。谢谢 (2认同)