![](/img/trans.png)
[英]Send JSON with array of objects as parameter in Alamofire with swift
[英]Swift - Serialize object containing array of objects and send with alamofire
我需要使用 Alamofire 發送帶有類作為參數的 post 請求。 我的班級包含其他班級的數組,因此我被卡住了。
class CreateRecordData{
var RecordDate: String?
var RecordType: String?
var SubType: String?
var Name: String?
var MovementRecords: [RecordMovementData]?
var MeasurementId: Int?
var SensorType: String?
}
class RecordMovementData{
var FrameRate: Int?
var JointType: String?
var Samples: [MovementSampleData]?
var RecordId: Int?
}
class MovementSampleData {
var Id: Int?
var TimeStamp: Int64?
var X: Float?
var Y: Float?
var Z: Float?
}
我是 swift 的新手,在一段時間內無法完成。
有什么方法可以解決這個問題。
我需要 CreateRecordData 類使用 alamofire 作為參數發送。
謝謝
添加 Encodable 后的類
class CreateRecordData: Encodable{
var RecordDate: String?
var RecordType: String?
var SubType: String?
var Name: String?
var AudioRecords: [AudioData]?
var MovementRecords: [RecordMovementData]?
var MeasurementId: Int?
var SensorType: String?
enum CodingKeys: String, CodingKey {
case RecordDate
case RecordType
case SubType
case Name
case AudioRecords
case MovementRecords
case MeasurementId
case SensorType
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(RecordDate, forKey: .RecordDate)
try container.encode(RecordType, forKey: .RecordType)
try container.encode(SubType, forKey: .SubType)
try container.encode(Name, forKey: .Name)
try container.encode(AudioRecords, forKey: .AudioRecords)
try container.encode(MovementRecords, forKey: .MovementRecords)
try container.encode(MeasurementId, forKey: .MeasurementId)
try container.encode(SensorType, forKey: .SensorType)
}
}
class RecordMovementData: Encodable{
var FrameRate: Int?
var JointType: String?
var Samples: [MovementSampleData]?
var RecordId: Int?
enum CodingKeys: String, CodingKey{
case FrameRate
case JointType
case Samples
case RecordId
}
func encode(to encoder: Encoder) throws{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(FrameRate, forKey: .FrameRate)
try container.encode(JointType, forKey: .JointType)
try container.encode(Samples, forKey: .Samples)
try container.encode(RecordId, forKey: .RecordId)
}
}
class MovementSampleData: Encodable {
var Id: Int?
var TimeStamp: Int64?
var X: Float?
var Y: Float?
var Z: Float?
enum CodingKeys: CodingKey{
case Id
case TimeStamp
case X
case Y
case Z
}
func encode(to encoder: Encoder) throws{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(Id, forKey: .Id)
try container.encode(TimeStamp, forKey: .TimeStamp)
try container.encode(X, forKey: .X)
try container.encode(Y, forKey: .Y)
try container.encode(Z, forKey: .Z)
}
}
還有一件事要提。 a 不要直接使用 CreateRecordData 作為參數。 我用該類的數據創建字典,如下所示:
let recordParams: [String:Any] = [
"RecordDate": recordData.RecordDate!,
"RecordType": recordData.RecordType!,
"SubType": recordData.SubType!,
"Name": recordData.Name!,
"AudioRecords": "",
"MovementRecords": recordData.MovementRecords!,
"MeasurementId": "\(recordData.MeasurementId!)",
"SensorType": recordData.SensorType!
]
AF.request("https://repac-fe-v1-webapp.azurewebsites.net/api/record",method: .post, parameters: recordParams, encoding: JSONEncoding.default, headers: header).responseJSON { response in
}
這也可能有問題
由於 JSON 中的鍵與模型屬性的名稱相同,因此您可以省略模型類中的CodingKeys
枚舉。 此外,由於您沒有在編碼中進行自定義操作,因此可以完全省略encode(to:)
函數的實現:
class AudioData: Encodable {
// This class is missing from the posted code
}
class CreateRecordData: Encodable {
var RecordDate: String?
var RecordType: String?
var SubType: String?
var Name: String?
var AudioRecords: [AudioData]?
var MovementRecords: [RecordMovementData]?
var MeasurementId: Int?
var SensorType: String?
}
class RecordMovementData: Encodable {
var FrameRate: Int?
var JointType: String?
var Samples: [MovementSampleData]?
var RecordId: Int?
}
class MovementSampleData: Encodable {
var Id: Int?
var TimeStamp: Int64?
var X: Float?
var Y: Float?
var Z: Float?
}
使用Encodable
對象作為參數發出 post 請求比您想象的要簡單。 您只需調用將encoder
而不是encoding
作為參數的request
方法:
let recordParams = CreateRecordData() // Set the class values here
AF.request(
"https://repac-fe-v1-webapp.azurewebsites.net/api/record",
method: .post,
parameters: recordParams,
encoder: JSONParameterEncoder.default,
headers: header
).responseJSON { response in
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.