ios - Parsing objects in Json array as a dictionary Swift -
so have json data formatted list of dictionaries, , have stored nsarray object, i'm unsure how convert each entry dictionary object when anyobject
the anyobject data formatted json dictionary
here code used create array
func startconnection(){ let urlpath: string = "http://api.mtgdb.info/search/omni" var url: nsurl = nsurl(string: urlpath)! var request: nsurlrequest = nsurlrequest(url: url) var connection: nsurlconnection = nsurlconnection(request: request, delegate: self, startimmediately: false)! connection.start() } func connection(connection: nsurlconnection!, didreceivedata data: nsdata!){ self.data.appenddata(data) } func connectiondidfinishloading(connection: nsurlconnection!){ var err: nserror var jsonresult: nsarray = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) as! nsarray var = 0; i<jsonresult.count; ++i{ ... } }
i tried sample code solve problem. first of run "http://api.mtgdb.info/search/omni" url in web browser , copy response paste "http://jsonlint.com", response valid , array of 8 dictionaries, id: 37113, 39932, 83737, 106426, 228247, 288937, 382286, 386302 -- 8 data.
in objective c, works perfect , same result web browser. in swift, behave weird, can't parse whole respose, half dictionary object of array. part of response,
printing description of jsonresult: ( { artist = "arnie swekel"; cardsetid = jud; cardsetname = judgment; colors = ( green, white ); convertedmanacost = 7; description = "trample\nphantom nishoba enters battlefield 7 +1/+1 counters on it.\nwhenever phantom nishoba deals damage, gain life.\nif damage dealt phantom nishoba, prevent damage. remove +1/+1 counter phantom nishoba."; flavor = ""; formats = ( { legality = legal; name = "odyssey block"; }, { legality = legal; name = legacy; }, { legality = legal; name = vintage; }, { legality = legal; name = freeform; }, { legal
i tried sample of code
class viewcontroller: uiviewcontroller, nsurlconnectiondelegate { var data:nsmutabledata! var arrvehicls:nsmutablearray! override func viewdidload() { super.viewdidload() self.data = nsmutabledata() self.arrvehicls = nsmutablearray() self.startconnection() } func startconnection(){ let urlpath: string = "http://api.mtgdb.info/search/omni" var url: nsurl = nsurl(string: urlpath)! var request: nsurlrequest = nsurlrequest(url: url) var connection: nsurlconnection = nsurlconnection(request: request, delegate: self, startimmediately: false)! connection.start() } func connection(connection: nsurlconnection!, didreceivedata data: nsdata!){ self.data.appenddata(data) } func connectiondidfinishloading(connection: nsurlconnection!) { var err: nserror var jsonresult:nsarray = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) as! nsarray var = 0; i<jsonresult.count; ++i { var dictresult = jsonresult.objectatindex(i) as! nsdictionary var vehicleinfo = vehicle() vehicleinfo.id = dictresult.valueforkey("id") as! int vehicleinfo.artist = dictresult.valueforkey("artist") as! string vehicleinfo.cardid = dictresult.valueforkey("cardsetid") as! string vehicleinfo.cardname = dictresult.valueforkey("cardsetname") as! string vehicleinfo.colors = dictresult.valueforkey("colors") as! nsarray vehicleinfo.details = dictresult.valueforkey("description") as! string vehicleinfo.flavour = dictresult.valueforkey("flavor") as! string vehicleinfo.formats = nsmutablearray() var arr = dictresult.valueforkey("formats") as! nsarray var j = 0; j<arr.count; ++i { var dictformats = arr.objectatindex(i) as! nsdictionary var formats = formats() formats.legality = dictformats.valueforkey("legality") as! string formats.name = dictformats.valueforkey("name") as! string vehicleinfo.formats.addobject(formats) } self.arrvehicls.addobject(vehicleinfo) } } }
Comments
Post a Comment