objective c - How to use JSON for an iPhone app? -
i trying make weather app, found great json weather api online. using
nsdata * data = [nsdata datawithcontentsofurl: [nsurl urlwithstring: absoluteurl]]; nserror * error; nsdictionary * json = [nsjsonserialization jsonobjectwithdata: data //1 options: kniloptions error: & error ]; nslog(@"%@", json); nslog([nsstring stringwithformat: @"location: %@", [json objectforkey: @"status"]]);
to data, wont work, log returns (null)
. can plase explain me how can strings , values of json file? thanks!
edit: change code line 3 (including) to:
nsdictionary * json = [nsjsonserialization jsonobjectwithdata:data options:0 error:&error]; nsarray *meta = json[@"objects"]; (nsdictionary *adict in meta) { nsdictionary *location = adict[@"location"]; nslog(@"%@", location); }
this nslog()
s locations in json response
.
if want city
, country
once can following:
nsdictionary *location = json[@"objects"][0][@"location"]; nsstring *country = location[@"country"]; nsstring *locality = location[@"locality"]; nslog(@"country: %@", country); nslog(@"locality: %@", locality);
output:
country: germany
locality: hauzenberg