ios - Parsing JSON using a Swift Dictionary -
i having hard time parsing json swift. have written function make request api , retrieves data json , converts dictionary. after trying use tableviewcontroller set each title , subtitle values received json. trying set title hometeam , subtitle awayteam. not know swift hoping help.
here json stored in dictionary:
dictionary = ["scoreboard": { gamescore = ( { game = { id = 35119; awayteam = { abbreviation = atl; city = atlanta; id = 91; name = hawks; }; date = "2017-04-07"; hometeam = { abbreviation = cle; city = cleveland; id = 86; name = cavaliers; }; location = "quicken loans arena"; time = "7:30pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35120; awayteam = { abbreviation = mia; city = miami; id = 92; name = heat; }; date = "2017-04-07"; hometeam = { abbreviation = tor; city = toronto; id = 81; name = raptors; }; location = "air canada centre"; time = "7:30pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35121; awayteam = { abbreviation = nyk; city = "new york"; id = 83; name = knicks; }; date = "2017-04-07"; hometeam = { abbreviation = mem; city = memphis; id = 107; name = grizzlies; }; location = "fedex forum"; time = "8:00pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35122; awayteam = { abbreviation = det; city = detroit; id = 88; name = pistons; }; date = "2017-04-07"; hometeam = { abbreviation = hou; city = houston; id = 109; name = rockets; }; location = "toyota center"; time = "8:00pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35123; awayteam = { abbreviation = sas; city = "san antonio"; id = 106; name = spurs; }; date = "2017-04-07"; hometeam = { abbreviation = dal; city = dallas; id = 108; name = mavericks; }; location = "american airlines center"; time = "8:30pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35124; awayteam = { abbreviation = nop; city = "new orleans"; id = 110; name = pelicans; }; date = "2017-04-07"; hometeam = { abbreviation = den; city = denver; id = 99; name = nuggets; }; location = "pepsi center"; time = "9:00pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35125; awayteam = { abbreviation = min; city = minnesota; id = 100; name = timberwolves; }; date = "2017-04-07"; hometeam = { abbreviation = uta; city = utah; id = 98; name = jazz; }; location = "vivint smart home arena"; time = "9:00pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35126; awayteam = { abbreviation = okl; city = "oklahoma city"; id = 96; name = thunder; }; date = "2017-04-07"; hometeam = { abbreviation = phx; city = phoenix; id = 104; name = suns; }; location = "talking stick resort arena"; time = "10:00pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; }, { game = { id = 35127; awayteam = { abbreviation = sac; city = sacramento; id = 103; name = kings; }; date = "2017-04-07"; hometeam = { abbreviation = lal; city = "los angeles"; id = 105; name = lakers; }; location = "staples center"; time = "10:30pm"; }; iscompleted = false; isinprogress = false; isunplayed = true; quartersummary = "<null>"; } ); lastupdatedon = "<null>"; }]
here have setting title , subtitle in swift:
override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "nbascore", for: indexpath) // configure cell... if let scoreboard = d.dictionary["scoreboard"] as? [string:anyobject] { if let gamescore = scoreboard["gamescore"] as? [string:anyobject] { if let game = gamescore["game"] as? [string:anyobject] { if let awayteam = game["awayteam"] as? string { cell.textlabel?.text = awayteam } } } } return cell }
best create struct add variable's properties need after make array struct parse array of json using loop , append them newly array have created can access properties using dot notation inside cellforrow function
Comments
Post a Comment