我是 swift 的新手,正在尝试使用具有某些属性的 api 发出 http post 请求。 我定义了以下类
类顺序{
var address1 : String?
var address2 : String?
var cellPhone : String?
var city : String?
var countryName : String?
var orderDate : String?
var orderStatus : Int?
var orderedProductList : Array<OrderedProduct>?
var paymentTransactionId : String?
var state : String?
var zip : String?
var countryId : Int?
var orderId : Int?
var orderTotal : Int?
var paymentMethodId : Int?
var userId : Int?
init(address1:String?, address2:String?, cellPhone:String?, city:String?, countryName:String?, orderDate:String?,orderStatus:Int?,orderedProductList:Array<OrderedProduct>?, paymentTransactionId:String?, state:String?, zip:String?, countryId:Int?, orderId:Int?, orderTotal:Int?, paymentMethodId:Int?, userId:Int?)
{
self.address1 = address1
self.address2 = address2
self.cellPhone = cellPhone
self.city = city
self.countryName = countryName
self.countryId = countryId
self.orderDate = orderDate
self.orderStatus = orderStatus
self.paymentTransactionId = paymentTransactionId
self.state = state
self.zip = zip
self.orderId = orderId
self.orderTotal = orderTotal
self.paymentMethodId = paymentMethodId
self.userId = userId
self.orderedProductList = orderedProductList
}
}
订单实例是:
var totalOrderInfo = Order(address1: address, address2: apartment, cellPhone: phone, city: city, countryName: cName, orderDate: "\(year)-\(month)-\(day)T\(hour):\(minutes):\(seconds)", orderStatus: 1, orderedProductList: orderedProductList, paymentTransactionId: transctionID, state: state, zip: zip, countryId: cId, orderId: 0, orderTotal: returnValue1, paymentMethodId: 1, userId: userID)
totalOrderInfo 的 JSON 表示如下:
{"address1":"Mirpur","address2":"D6, f8","cellPhone":"01852540565","city":"fghff","countryName":"Bangladesh","orderDate":"2017-02-25T11:28:24","orderStatus":1,"orderedProductList":[{"discount":0.0,"orderDetailId":0,"price":30000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":{"isBook":false,"typeId":0},"productId":5,"productViews":0},{"discount":0.0,"orderDetailId":0,"price":50000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":{"isBook":false,"typeId":0},"productId":8,"productViews":0},{"discount":0.0,"orderDetailId":0,"price":2000.0,"quantity":1,"shippingCharge":50.0,"supplierId":0,"tax":0.0,"type":{"isBook":false,"typeId":0},"productId":9,"productViews":0}],"paymentTransactionId":"1215455638874521","state":"fyy","zip":"4525","countryId":23,"orderId":0,"orderTotal":82000.0,"paymentMethodId":1,"userId":0}
如何序列化 totalOrderInfo 实例并获取上述 JSON?
谢谢
最佳答案
您可以使用库,例如:Object Mapper
在你的情况下你的类(class) Order
会是这样的:
class Order: Mappable {
var address1 : String?
var address2 : String?
var cellPhone : String?
var city : String?
var countryName : String?
var orderDate : String?
var orderStatus : Int?
var orderedProductList : Array<OrderedProduct>?
var paymentTransactionId : String?
var state : String?
var zip : String?
var countryId : Int?
var orderId : Int?
var orderTotal : Int?
var paymentMethodId : Int?
var userId : Int?
init?(map: Map){
}
init(address1:String?, address2:String?, cellPhone:String?, city:String?, countryName:String?, orderDate:String?,orderStatus:Int?,orderedProductList:Array<OrderedProduct>?, paymentTransactionId:String?, state:String?, zip:String?, countryId:Int?, orderId:Int?, orderTotal:Int?, paymentMethodId:Int?, userId:Int?)
{
self.address1 = address1
self.address2 = address2
self.cellPhone = cellPhone
self.city = city
self.countryName = countryName
self.countryId = countryId
self.orderDate = orderDate
self.orderStatus = orderStatus
self.paymentTransactionId = paymentTransactionId
self.state = state
self.zip = zip
self.orderId = orderId
self.orderTotal = orderTotal
self.paymentMethodId = paymentMethodId
self.userId = userId
self.orderedProductList = orderedProductList
}
mutating func mapping(map: Map){
address1 <- map["address1"]
address2 <- map["address2"]
cellPhone <- map["cellPhone"]
city <- map["city"]
countryName <- map["countryName"]
countryId <- map["countryId"]
orderDate <- map["orderDate"]
orderStatus <- map["orderStatus"]
paymentTransactionId <- map["paymentTransactionId"]
state <- map["state"]
zip <- map["zip"]
orderId <- map["orderId"]
orderTotal <- map["orderTotal"]
paymentMethodId <- map["paymentMethodId"]
userId <- map["userId"]
orderedProductList <- map["orderedProductList"]
}
}
因为你还有一个 Array<OrderedProduct>
在您的代码中,您必须对 OrderedProduct
执行相同的操作类。
之后,您可以使用以下方法将模型对象转换为 JSON 字符串:
let order = Order(address1, address2.......)
let jsonString = order.toJSONString(prettyPrint: true)
如果你想了解更多关于这个库的信息以及如何安装它,你可以在他们的 Github Project 查看官方文档。页
关于对象的json序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42736749/