Json:解析_超爽的一次,self.Pride
Model
import UIKit
class HomePageModel: NSObject {
var code: NSNumber?
var msg: String?
var data: HPageDataModel?
}
class HPageDataModel: NSObject{
var banner: [HPDataBannerModel]?
var catalog: String?
var dinnerList: [HPDataDinnerModel]?
}
class HPDataBannerModel: NSObject{
var bannerId: NSNumber?
var type: NSNumber?
var title: String?
var subtitle: String?
var banner_image: String?
var _description: String?
//跟系统的description方法冲突
func setDescription(description: String){
self._description = description
}
}
class HPDataDinnerModel: NSObject{
var dinnerType: NSNumber?
var dinnerId: NSNumber?
var type: NSNumber?
var title: String?
var datetime: String?
var district: String?
var imageurl: String?
var price: String?
var minCount: NSNumber?
var maxCount: NSNumber?
var orderCount: NSNumber?
var endOrderTime: String?
var chefId: NSNumber?
var chefImageurl: String?
var chefName: String?
var chefShopName: String?
var themeDinnerId: NSNumber?
var themeDinnerTitle: String?
var themeDinnerMinPrice: NSNumber?
var themeDinnerMaxPrice: NSNumber?
var themeDinnerImageurl: String?
var themeDinnerDistrict: String?
var themeDinnerStartTime: String?
var themeDinnerEndTime: String?
var themeDinnerChefId: NSNumber?
var themeDinnerChefImageurl: String?
var themeDinnerChefName: String?
var themeDinnerChefShopName: String?
var themeDinnerUnit: String?
}
解析数据
extension HomePageViewController{
func succeedDownloading(downloader: DownloadAlamo, finishWithData data: NSData) {
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
print(string)
let jsonData = try! NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
if jsonData.isKindOfClass(NSDictionary.self){
let dictionary = jsonData as! NSDictionary
let dataDictionary = dictionary["data"] as! Dictionary<String,AnyObject>
let bannerArray = dataDictionary["banner"] as! Array<Dictionary<String,AnyObject>>
var arrayBanner = [HPDataBannerModel]()
let dinnerArray = dataDictionary["dinnerList"] as! Array<Dictionary<String,AnyObject>>
var arrayDinner = [HPDataDinnerModel]()
for partDictionary in bannerArray{
let modelOne = HPDataBannerModel()
modelOne.setValuesForKeysWithDictionary(partDictionary)
arrayBanner.append(modelOne)
}
for partTwo in dinnerArray {
let modelTwo = HPDataDinnerModel()
modelTwo.setValuesForKeysWithDictionary(partTwo)
arrayDinner.append(modelTwo)
}
let dataModel = HPageDataModel()
dataModel.banner = arrayBanner
dataModel.dinnerList = arrayDinner
dataModel.catalog = dataDictionary["catalog"] as? String
mainModel = HomePageModel()
mainModel.code = dictionary["code"] as? NSNumber
mainModel.msg = dictionary["msg"] as? String
mainModel.data = dataModel
print("succeedDownloading")
print(mainModel.code)
print(mainModel.data?.banner)
dispatch_async(dispatch_get_main_queue(), {
self.tableView?.reloadData()
})
}
}