My Swift code output this:
{
date = "2016-08-25 20:46:35 +0000";
observation = "";
products = (
{
name = cafe;
quantity = 1;
},
{
name = cappuccino;
quantity = 1;
}
);
room = Platinium;
}
But I need this JSON
{
"date": "2016-08-25 20:46:35 +0000",
"observation": "",
"products": [{
"name": "cafe",
"quantity": 1
}, {
"name": "cappuccino",
"quantity": 1
}],
"room": "Platinium"
}
My classes is (Products and Order):
class Products {
var name: String = ""
var quantity: Int = 0
init(name: String, quantity: Int) {
self.name = name
self.quantity = quantity
}
}
class Order{
var room: String = ""
var products: Array<Products> = []
var observations: String = ""
var date: NSDate! = nil
init(room: String, products:Array<Products>, observations: String, date: NSDate){
self.room = room
self.products = products
self.observations = observations
self.date = date
}
}
And this Swift 2 code to transform in json.. After I'm using a try/catch to transform in json with NSJSONSerialization
let para:NSMutableDictionary = NSMutableDictionary()
let prodArray:NSMutableArray = NSMutableArray()
para.setValue(String(receivedString), forKey: "room")
para.setValue(observationString, forKey: "observation")
para.setValue(stringDate, forKey: "date")
for product in products{
let prod: NSMutableDictionary = NSMutableDictionary()
prod.setValue(product.name, forKey: "name")
prod.setValue(product.quantity, forKey: "quantity")
prodArray.addObject(prod)
}
para.setObject(prodArray, forKey: "products")
Someone help me? Thank you very much!!