objective c - iOS and JSON nesting issue -
i working on app game server company , part of app requires user see list of or game servers , whether or not online, offline, how many players on them, server name, etc. data found in json file hosted on web updated mysql database.
using code below, can't seem working. now, please understand 1 of first times working json , have no choice client requested. talked partner couldn't seem debug issue himself.
the error like:
no visible @interface 'nsarray' declares selector 'objectforkey:'
i've tried several versions of code, no success.
it appreciated if please me debug code , working along commented out section near bottom updating tableviewcells server name, players online, , status (0=offline, 1=online, 2=busy, 3=suspended, -1=unable start).
please note format of json file must remain , possible make minor changes.
thank you, michael s.
my header file:
my main file:
my json file:
{ "status": "ok", "error": "", "debug": "2 server(s)", "result": { "servers": [ { "id": 1, "title": "test", "players": 0, "slots": 10, "status": 3 }, { "id": 2, "title": "creative spawn", "players": 0, "slots": 5, "status": -1 } ] } }
the block gives me error is:
nsarray *serverresults = [[news objectforkey:@"result"] objectforkey:@"servers"]; if ([[[serverresults objectatindex:indexpath.row] objectforkey:@"status"] isequal:@"1"]) { serverplayers.text = @"10000000"; }
the error points out working on nsarray instead of nsdictionary. there no method objectforkey
defined on nsarray. debug webservice response , check data types.
regarding @ output json should that:
nsdictionary (keys: status, error, debug, result, servers) servers nsarray has nsdictionaries elements.
to title of server:
nsdictionary *resultdict = [news objectforkey:@"result"]; nsarray *servers = [resultdict objectforkey:@"servers"]; nsdictionary *firstserver = [servers objectatindex:0]; // fetch here first server nsstring *titleoffirstserver = [firstserver objectforkey:@"title"]; nsnumber *statusoffirstserver = [nsnumber numberwithint:[[firstserver objectforkey:@"status"] intvalue];
to iterate on servers should that:
nsdictionary *resultdict = [news objectforkey:@"result"]; nsarray *servers = [resultdict objectforkey:@"servers"]; for(nsdictionary *server in servers) { nsstring *title = [server objectforkey:@"title"]; nsnumber *status = [nsnumber numberwithint:[[server objectforkey:@"status"] intvalue]; nslog(@"%@%d", title, [status intvalue]); }
Comments
Post a Comment