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

心不动则不痛 提交于 2019-12-13 03:35:25

问题


I have tried to create & send array of dictionary JSON format to 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. 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 "[]"
} }

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. Please help i am stuck?


回答1:


Simply you can do

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



回答2:


Object Mapping, you can use ObjectMapper library.

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

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

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:

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


来源:https://stackoverflow.com/questions/46949490/how-can-i-make-send-array-of-dictionary-json-format-send-to-server-using-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!