我该如何制作&在Swift 3中使用Alamofire发送字典数组(JSON格式)发送到服务器 [英] How Can I make & send array of dictionary (JSON Format ) send to server using Alamofire in swift 3

查看:74
本文介绍了我该如何制作&在Swift 3中使用Alamofire发送字典数组(JSON格式)发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建&将字典JSON格式的数组发送到Alamofire.

I have tried to create & send array of dictionary JSON format to Alamofire.

 {
"attendance": [{
    "attndid":"0",
    "psngrtype": "student",
    "date":currentdate,
    "cuid": "25",
    "enttid": "21",

}] }

在这里,我使用了tableview,在上面的"cuid"& "enttid"我从可选的tableview单元格数据中赋值.其余的是恒定的.如果我选择一个tableview单元格数据,我将传递一个字典数组.和两个数组,如果我选择两个单元格等. 而且我正在使用下面的代码,但我遇到了创建字典格式的问题. 我的代码:

Here, I am used tableview, In the above "cuid" & "enttid" I giving value from selectable tableview cell data. remaining are constant. I am passing one array of dictionary, if i select one tableview cell data. and two array, if i select two cells.etc.. and I am using below code and I get but getting issue create dictionary format. My code:

 let arrayOfDictionaries: [[String:AnyObject]] =
        [
        ["psngrtype":"student" as AnyObject,
         "attndid": "0" as AnyObject ,
         "cuid":stdpasngerid as AnyObject,
         "enttid": entitID as AnyObject,
         "attnddate":CurrentDate as AnyObject ]]

          print(arrayOfDictionaries.toJSONString())

extension Collection where Iterator.Element == [String:AnyObject] {
func toJSONString(options: JSONSerialization.WritingOptions = .prettyPrinted) -> String {
    if let arr = self as? [[String:AnyObject]],
        let dat = try? JSONSerialization.data(withJSONObject: arr, options: options),
        let str = String(data: dat, encoding: String.Encoding.utf8) {
        return str
    }
    return "[]"
} }

输出:

[{
"enttid" : "1",
"psngrtype" : "student",
"attnddate" : "10-26-2017",
"attndid" : "0",
"cuid" : "25" }]

我想添加第一个像json格式.如果我选​​择多个tableview单元,则添加多个数组.请帮我卡住吗?

And I want to add first one like json format.and add multiple array if i select more than one tableview cell. Please help i am stuck?

推荐答案

对象映射,您可以使用ObjectMapper库.

Object Mapping, you can use ObjectMapper library.

像这样创建对象出勤

class Attendance: Mappble {
 var objects: [AttendancesAttributes] = []
 init () { }
 required init?(map:Map) { }

 func mapping(map: Map){
   objects <- map["attendance"]
 }
}

然后创建您的AttendancesAttributes对象

Then creates your AttendancesAttributes object

class AttendancesAttributes: Mappable {
    var id: String
    ....
   func mapping(map: Map) {
      //do the mapping with your dictionary
   }
}

然后在您的VC中,我想您可以做到

Then in your VC I guess you can do

var attendance: Attendance = Attendance()
func (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     //check if your object is or not in your array.
   attendance.objects.append(yourTableViewFeeder[indexPath.row])
}

最后在您的API客户端中:

and finally in your API Client:

 func sendAttendanceInformationsToServer(attendance: Attendance) {
   Alamofire.request(url, method, parameters: attendance.toJSON(), encoding: JSONEncoding.default, headers: ["":""]).responseJSON (completionHandler: { //deal your response here
  })
 }

这篇关于我该如何制作&amp;在Swift 3中使用Alamofire发送字典数组(JSON格式)发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆