简体   繁体   English

如何在Swift 3中使用Alamofire制作并发送字典数组(JSON格式)发送到服务器

[英]How Can I make & send array of dictionary (JSON Format ) send to server using Alamofire in swift 3

I have tried to create & send array of dictionary JSON format to Alamofire. 我尝试创建字典JSON格式的数组并将其发送到Alamofire。

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

}] }

Here, I am used tableview, In the above "cuid" & "enttid" I giving value from selectable tableview cell data. 在这里,我使用了tableview,在上面的“ cuid”和“ enttid”中,我从可选tableview单元格数据中给出了值。 remaining are constant. 其余的都是常数。 I am passing one array of dictionary, if i select one tableview cell data. 如果我选择一个tableview单元格数据,则传递一个字典数组。 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 "[]"
} }

output: 输出:

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

And I want to add first one like json format.and add multiple array if i select more than one tableview cell. 我想添加第一个像json格式。如果我选​​择多个tableview单元,则添加多个数组。 Please help i am stuck? 请帮我卡住吗?

Simply you can do 只需您即可

var finalArray:[String:String] = [:]
finalArray["Attandance"] = arrayOfDictionaries.toJSONString()

Object Mapping, you can use ObjectMapper library. 对象映射,可以使用ObjectMapper库。

Create object Attendance like 创建像这样的对象出勤

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

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

Then creates your AttendancesAttributes object 然后创建您的AttendancesAttributes对象

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

Then in your VC I guess you can do 然后,在您的VC中,我想您可以

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])
}

and finally in your API Client: 最后在您的API客户端中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速发送带有对象数组的JSON作为Alamofire中的参数 - Send JSON with array of objects as parameter in Alamofire with swift 如何使用alamofire快速发布字典数组(json对象)? - How to post an array of dictionary (json object), in swift using alamofire? 如何在 Swift 中将数组作为参数发送到 Alamofire - how to send array to Alamofire as a Paremeter in Swift 如何使用Swift Alamofire在REST API中发送HTML标签字符串? - How can I send HTML tag string in REST API by using Swift Alamofire? 如何使用Alamofire和Mailgun使用Swift发送电子邮件? - How to send email using Alamofire and Mailgun with Swift? 我如何在Alamofire发布请求中将JSON变量作为参数发送 - How can i send a JSON variable as param in alamofire post request 是否可以使用 Alamofire swift 将数组和字典都发送到 api 命中时间的参数中? - Is it possible to send Array and Dictionary both into parameters at api hitting time using Alamofire swift? 如何使用Alamofire multipart在params中发送数组 - How to send array in params using Alamofire multipart 如何快速使用 alamofire 将任何文件上传到服务器 - How can I upload any file to server using alamofire in swift 如何使用 swift 4 从数据中使用 Alamofire multipart 在 params 中发送数组 - How to send array in params using Alamofire multipart from data using swift 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM