ios - How to Grab Specific Objects within a Web Service -
i'm using github job web service. here link service:https://jobs.github.com/api
now i'm able retrieve data inside specific url such one:
@"http://jobs.github.com/positions.json?description=python&location=new+york" i'm able json data following code:
- (void)viewdidload { [super viewdidload]; nsstring *urlstring = @"http://jobs.github.com/positions.json?description=python&location=new+york"; nsurl *url = [nsurl urlwithstring:urlstring]; nsdata *data = [nsdata datawithcontentsofurl:url]; nsarray *json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; nslog(@"%@", json); // additional setup after loading view. }
now fine, but when try this:
nsarray *items = [[nsarray alloc] init]; items = json[@"description"]; nslog(@"%@", items); i sigabrt , error receive following:
jobsearch[1249:70b] * terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[__nscfarray objectforkeyedsubscript:]: unrecognized selector sent instance 0x8a82930'
i'm confused because other web services i've used i'm able things this, , "description" should item should able grab. don't seem getting anywhere...
all appreciated, in advance.
first doesn't make sense alloc/init array, because you're overwriting next line
nsarray *items = [[nsarray alloc] init]; items = json[@"description"]; also, jsonobjectwithdata returns on object of type id, you're assigning nsdictionary, assumption proves wrong.
nsdictionary *json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; apparently method returning array, hence calling json[@"description"] invalid. try using this:
nsarray *json = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; nsstring item = [json objectatindex:0]; // grab item array without guarantee work. suggest first check kind of object returned, perhaps iskindofclass, see if nsdictionary or array.
Comments
Post a Comment