// converting JSON Data to JSON String
// function call
self.deviceDict(2, mac: "22:00:88:p1:uu", sn: "123")
//---------------------------------------//
{
"devices" : [
{
"id" : 2,
"mac" : "22:00:88:p1:uu",
"sn" : "123"
}
]
}
//---------------------------------------//
func deviceDict(id:Int,mac:String,sn:String)-> String {
let dict : NSMutableDictionary = NSMutableDictionary()
let Arr : NSMutableArray = []
let finaldict : NSMutableDictionary = NSMutableDictionary()
dict.setValue(id, forKey: "id")
dict.setValue(mac, forKey: "mac")
dict.setValue(sn, forKey: "sn")
Arr.addObject(dict)
finaldict.setValue(Arr, forKey: "devices")
do {
let jsonData : NSData = try! NSJSONSerialization.dataWithJSONObject(finaldict, options: NSJSONWritingOptions.PrettyPrinted)
//Convert back to string. Usually only do this for debugging
if let JSONString = String(data: jsonData, encoding: NSUTF8StringEncoding)
{
return JSONString
}
} // end of do
return "Error"
}// end of function