我有一個從服務器獲取的json響應,應該將其解析爲字典並將其轉換爲Swift中的NSObjects。我怎樣才能做到這一點。將JSON轉換爲Swift中的對象
func convertJSONToObjects(object:Dictionary<String,AnyObject>?) -> CommonResponse{
var response:CommonResponse;
if(object != nil){
response = CommonResponse()
var responseObject = object //It is dictionary
var responseHeader = object["responseHeader"] //error
response.status = responseHeader["status"] as Int //error
//response.status is Int
}
return response
}
var parsedJson = NSJSONSerialization.JSONObjectWithData(object as NSData, options: NSJSONReadingOptions.MutableContainers, error: &parseError) as NSDictionary
調用函數
convertJSONToObjects(parsedJson)
我可以推薦https://github.com/SwiftyJSON/SwiftyJSON – 2014-10-09 09:25:17