我的问题是我有一个使用Alamofire进行REST业务的应用程序,并且通过post和get动作可以正常运行。但在一个方法中,我需要发送一个复杂的对象,它被设置为null,我改变了一些,它有点工作,但不完全。这里看看我的意思:
import UIKit
import ObjectMapper
open class CashSale: Sale, CustomStringConvertible {
open var paymentCurrencyType: CurrencyType!
open var changeCurrencyType: CurrencyType!
open var amountReceived: Float!
lazy open var jsonObject: [String:Any] = [:]
public init (paymenttype:CurrencyType, changetype:CurrencyType, amount:Float, sale:Sale)
{
super.init(dateTime: sale.dateTime, guid: sale.guid, saledescription: sale.factPaymentType, amount:sale.amount, saleStatus: sale.saleStatus, paymentType: sale.paymentType, employee: sale.employee, soldItems: sale.soldItems, pump: sale.pump , gasSaleType: sale.gasSaleType)
self.amountReceived = amount
self.changeCurrencyType = changetype
self.paymentCurrencyType = paymenttype
self.finishJson()
}
func finishJson() {
jsonObject["paymentCurrencyType"] = paymentCurrencyType
jsonObject["changeCurrencyType"] = changeCurrencyType
jsonObject["amountReceived"] = amountReceived
jsonObject["devicename"] = devicename
jsonObject["id"] = id
jsonObject["dateTime"] = (self.dateTime == "") ? "2017-08-10":self.dateTime
jsonObject["guid"] = guid
jsonObject["factPaymentType"] = factPaymentType
jsonObject["amount"] = amount
jsonObject["saleStatus"] = saleStatus
jsonObject["paymentType"] = paymentType
jsonObject["employee"] = ["id":self.employee.id, "name":self.employee.name, "pin": self.employee.pin, "employeeType": self.employee.employeeType, "pumps": self.employee.pumps]
jsonObject["gasSaleType"] = gasSaleType
jsonObject["soldItems"] = ["id":self.soldItems[0].id, "saleItem":["id":self.soldItems[0].saleItem.id, "itemType":self.soldItems[0].saleItem.itemType, "pricePerUnit":self.soldItems[0].saleItem.pricePerUnit, "barcode":self.soldItems[0].saleItem.barcode ?? "-1"], "quantity":self.soldItems[0].quantity, "localidadId":self.soldItems[0].localidadId]
jsonObject["pump"] = ["id": self.pump.id, "status": self.pump.status, "gasTypes":self.pump.gasTypes]
jsonObject["special"] = special
jsonObject["invoiceDat"] = ["accountNum":(self.invoiceDat.accountNum == "" || self.invoiceDat.accountNum == nil) ? "-1":self.invoiceDat.accountNum!, "clientId":(self.invoiceDat.clientId == "" || self.invoiceDat.clientId == nil) ? "-1":self.invoiceDat.clientId!]
jsonObject["finishType"] = finishType
}
}
电话就在这里:
//Mensaje que manda una CashSale
open func SendCashVenta(_ venta:CashSale, ip:String, callBack:(()->Void)?, incorrecto:((_ Title:String, _ Error:String)->Void)?)
{
venta.devicename = DeviceName
venta.invoiceDat = InvoiceData(accountnum: "", clientid: "")
let address:String = "http://\(ip)/api/InitCashSales"
let debugMsg = "Cash sale initiated"
print(venta.jsonObject)
let serial = JSONSerializer.toJson(venta)
// let _parametros = JSONSerializer.toDictionary(serial) as! Parameters
//debugPrint(_parametros)
self.alamoFireManager!.request(address, method: .post, parameters: venta.jsonObject, encoding: URLEncoding.httpBody)
.responseJSON { response in
self.responseEmptyCallback(response, result: response.result, callBack: callBack, incorrecto: incorrecto, debugMsg: debugMsg)
}
}
在employee对象中,嵌套工作正常,获取值并发送到REST,但是如果soldItems是具有嵌套数组的对象数组,则它不会发送任何内容而我的REST类崩溃。所以我的问题是如何用更多嵌套对象对一个对象进行jsonify?像我一样。感谢。