【问题标题】:How Can I make & send array of dictionary (JSON Format ) send to server using Alamofire in swift 3如何在 swift 3 中使用 Alamofire 制作和发送字典数组(JSON 格式)发送到服务器
【发布时间】:2017-10-26 08:38:15
【问题描述】:

我尝试创建字典 JSON 格式数组并将其发送到 Alamofire。

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

}] }

在这里,我使用了tableview,在上面的“cuid”和“enttid”中,我从可选的tableview单元格数据中给出了价值。剩余的都是常数。如果我选择一个表格视图单元格数据,我将传递一组字典。和两个数组,如果我选择两个cells.etc .. 我正在使用下面的代码,但我得到了创建字典格式的问题。 我的代码:

 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 单元格,则添加多个数组。请帮助我卡住了?

【问题讨论】:

  • 不相关,但服务器根本不关心漂亮打印的 JSON。
  • @vadian 谢谢!所以,你能告诉我一些例子吗?如果不是我的。
  • 我只是想把默认的写作选项改成[]
  • @vadian 是的,我做到了。但服务器不接受我的格式。它接受上述格式。那是我的问题。

标签: ios arrays json swift alamofire


【解决方案1】:

你可以做到

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

【讨论】:

  • 是的,我试过这个,但删除 \n.. ' print(finalArray) ' 输出有问题:' ["attendance": "[\n {\n \"enttid\" : \"1\",\n \"status\" : \"\",\n \"clsid\" : \"45\",\n \"psngrtype\" : \"student\",\n \ "attnddate\" : \"10-26-2017\",\n \"isactive\" : \"true\",\n \"psngrid\" : \"\",\n \"attndid\" : \"0\",\n \"cuid\" : \"25\"\n }\n]"] ' 从 [string:string] 中删除 \n 的问题。我坚持了下来。谢谢
  • 要从字符串中删除 \n,您可以使用:let dictString = String(yourJsonString.characters.filter { !”\n”.characters.contains($0) })
【解决方案2】:

对象映射,可以使用ObjectMapper库。

创建对象出勤喜欢

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

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

然后创建您的 AttendancesAttributes 对象

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

那么在你的 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])
}

最后在您的 API 客户端中:

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

【讨论】:

  • 我安装了 pod of object Mapper。 Next 当我创建对象类时,它显示的错误是 Use of Undeclared type Mappble。代码:'类出勤:Mappble { var objects:[AttendancesAttributes] = [] init(){}需要init?(map:Map){} func mapping(map:Map){objects
  • 在您的文件中导入对象映射器。
  • 你能告诉我这个方法的一些例子吗' class AttendancesAttributes: Mappable { var id: String = "" func mapping(map: Map) { //用你的字典做映射 } } ' 错误:显示不确认协议。我正在使用 UISwitch 进入 tableview 单元格,开关是 ON (默认)。当用户关闭时,我正在获取行数据的 ID。我怎样才能添加到这里。请帮助我卡住了。
  • 在 AttendanceAttributes 中添加您需要的所有变量。然后实现协议: func mapping(map: Map) AND required init?(map: Map) (你错过了初始化程序)。你能告诉我 UITableView/UITableViewCell 的代码吗?我很乐意为您提供帮助。
  • ' 类出勤:可映射 { var 对象:[AttendancesAttributes] = [] init () { } 需要初始化?(map:Map) { } func mapping(map: Map){ objects
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多