arrays - How to parse Dictionary value and pass into class modal in swift? -
my json data
{ "addon_items" : [ { "aname" : "", "id" : "2588", "name" : "plain nan", "order" : "1", "aid" : "259", "sub_add_items" : "", "icon" : "", "status" : "1", "next" : "0", "price" : "0.60" }, { "aname" : "", "id" : "2589", "name" : "pitta bread", "order" : "2", "aid" : "259", "sub_add_items" : "", "icon" : "", "status" : "1", "next" : "0", "price" : "0.00" } ], "addon" : { "description" : "please choose nan bread", "aname" : "", "id" : "259", "icon" : "", "limit" : "1", "special_addon" : "", "next" : "165" } }
i created 3 class models named addonresponse, addon, addonitems this:
addonresponse class model
class addonresponse { var addon: [string: anyobject]? var addonitems: array<anyobject>? init(addon:[string: anyobject]?,addonitems: array<anyobject>?){ self.addon = addon self.addonitems = addonitems } }
addon class model
class addon { var id: int? var icon: string? var desc: string? var limit: int? var next: int? var aname: string? var specialaddon: int? init(id: int?,icon: string?,desc: string?,limit: int?,next: int?,aname: string?,specialaddon: int?){ self.id = id self.icon = icon self.desc = desc self.limit = limit self.next = next self.aname = aname self.specialaddon = specialaddon } }
addonitems class model
class addonitems { var id: int? var aid: int? var name: string? var price: int? var order: int? var status: int? var next: int? var aname: string? var subadditems: int? var icon: string? init(id: int?,aid: int?,name: string?,price: int?,order: int?,status: int?,next: int?,aname: string?,subadditems: int?,icon: string?){ self.id = id self.aid = aid self.name = name self.price = price self.order = order self.status = status self.next = next self.aname = aname self.subadditems = subadditems self.icon = icon } }
now fetching json data using alamofire when passing data addon model using object getting nil value.
var addonresponses = [addonresponse]() alamofire.request(.get, myaddonurl) .validate() .responsejson { response in switch response.result { case .success: if let value = response.result.value{ let json = json(value) print(json) print(json["addon"].arrayvalue) let addonres = addonresponse(addon: json["addon"].object as? [string : anyobject], addonitems: json["addon_items"].arrayobject) print(addonres.addon)
the addon
, addonitems
data coming here in class model. when passing data addon modal here getting nil..
(_,content) in json["addon"]{ let addon = addon(id: int(content["id"].stringvalue), icon: content["icon"].string, desc: content["description"].string, limit: int(content["limit"].stringvalue), next: int(content["next"].stringvalue), aname: content["aname"].string, specialaddon: int(content["special_addon"].stringvalue) ) print(addon.desc) /// nil value }
so how parse addon data , pass class model..
Comments
Post a Comment