ios - 如何使用 Struct Encodable、iOS、Swift 在 Alamofire 中发布没有键的对象数组
问题描述
我想通过发布以下正文Alamofire
。这是确切的帖子正文。我已经用邮递员测试过了
[
{
"keyone":{
"name":"abc",
"value":"1"
},
"keytwo":{
"age":"23",
"id":"9812fvereaere112"
},
"keythree":{
"expired":true,
"freshuser":false
},
"keyfour":{
"license":true
}
}
]
我Struct
为此创建了一个。
// MARK: - Keyfour
struct Keyfour: Codable {
let license: Bool
}
// MARK: - Keyone
struct Keyone: Codable {
let name, value: String
}
// MARK: - Keythree
struct Keythree: Codable {
let expired, freshuser: Bool
}
// MARK: - Keytwo
struct Keytwo: Codable {
let age, id: String
}
然后我从这些对象创建了一个数组,并尝试将其用作 Alamofire.but 的参数,
Alamofire
但[String: Any]
我应该发送[Dictionary<String, Any>]
. 我怎样才能做到这一点。
我试图将我的对象数组转换为字典,但使用以下方法失败
private func convertdocprocesstoDictonary(jsonstring: String) -> Dictionary<String, Any>? {
if let data = jsonstring.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? Dictionary<String, Any>
} catch let jsonerr {
print("\(jsonerr.localizedDescription)")
}
}
return nil
}
// Tried this with [Dictionary<String, Any>] as well.it fails too
let jdat = try JSONEncoder().encode(Array_of_Objects)
let jstring = String(data: jdat, encoding: .utf8)
let dic = convertdocprocesstoDictonary(jsonstring: jstring)
然后附加 dic 作为参数。什么都没有成功。希望您对此有所帮助。祝你有美好的一天,并保持安全。
解决方案
推荐阅读
- go - 成功连接 webscoket 后如何向特定 URI 发送消息?
- c# - HTTPWebRequest - GetResponse() - 在 SSIS C# 脚本中被远程主机强制关闭
- html - 通过 sqlite/flask 应用程序填充的 HTML 表显示原始代码而不是格式化代码
- algorithm - 最小生成树:prim and kruskal
- ios - SWIFT 如何从 GoogleMaps GMSAutocomplete 获取经纬度坐标?
- windows - 为什么 NRPT 规则不影响我的 Windows 10 机器上的 DNS 查询?
- android - 如何在颤动中从特定包中加载文件?
- php - 未定义变量:C:\xampp\htdocs\Auto\profile.php 中的 profile_data
- python - 使用python查找子字符串
- python - 如何在openpyXL中为特定列(D)添加下拉按钮并在选择一个选项时更改特定单元格的背景颜色