iphone - SBJson Parsing : How to drill down? -
i think need clarification on how drill down json values when using sbjson.
here link sample json need parse. https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=5en0wtzzxnlz3ongvybjfvpnpack21b0nws4c2r02mrfjogg&client_secret=uhd0nehnqmqzzwrj4x51dbqnwenp0d0yhg3wfvwj24vjhaoz&radius=4828&v=20120601
if @ parsed json looking location records. problem nothing getting brought in. so, question how can @ location records?
code follows.
- (nsstring *)stringwithurl:(nsurl *)url { nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:30]; // fetch json response nsdata *urldata; nsurlresponse *response; nserror *error; // make synchronous request urldata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:&error]; // construct string around data response return [[nsstring alloc] initwithdata:urldata encoding:nsutf8stringencoding]; } - (id) objectwithurl:(nsurl *)url { sbjsonparser *jsonparser = [[sbjsonparser new] autorelease]; nsstring *jsonstring = [self stringwithurl:url]; // parse json object return [jsonparser objectwithstring:jsonstring error:null]; } - (void) downloadjsonfeed { //set query nsstring *lat = [nsstring stringwithformat:@"%f", ad.locationmanager.location.coordinate.latitude]; nsstring *lon = [nsstring stringwithformat:@"%f", ad.locationmanager.location.coordinate.longitude]; nsstring *postvalues = [nsstring stringwithformat:@"https://api.foursquare.com/v2/venues/search?ll=%@,%@&client_id=%@&client_secret=%@&radius=4828&v=20120601",lat, lon, @"5en0wtzzxnlz3ongvybjfvpnpack21b0nws4c2r02mrfjogg", @"ehnqmqzzwrj4x51dbqnwenp0d0yhg3wfvwj24vjhaoz"]; //get server response id response = [self objectwithurl:[nsurl urlwithstring:postvalues]]; nsdictionary *location = [response objectforkey:@"location"]; //array json data nsmutablearray *jsondata = [[nsmutablearray alloc] init]; (nsdictionary *dict in location) { bathroom *bathroom = [[[bathroom alloc] init] autorelease]; bathroom.name = [dict objectforkey:@"name"]; bathroom.street = [dict objectforkey:@"address"]; bathroom.city = [dict objectforkey:@"city"]; bathroom.state = [dict objectforkey:@"state"]; bathroom.postal = [dict objectforkey:@"postalcode"]; bathroom.country = [dict objectforkey:@"country"]; [jsondata addobject:bathroom]; } //release dictionary //[dictionary release]; //now sort array distance nssortdescriptor *sortdescriptor; sortdescriptor = [[[nssortdescriptor alloc] initwithkey:@"name" ascending:yes] autorelease]; nsarray *sortdescriptors = [nsarray arraywithobject:sortdescriptor]; nsarray *sortedarray; sortedarray = [jsondata sortedarrayusingdescriptors:sortdescriptors]; //dataarray = [[nsmutablearray alloc] init]; //add objects data array [dataarray addobjectsfromarray:sortedarray]; //release json data [jsondata release]; }
the following section of code problem lies:
//get server response id response = [self objectwithurl:[nsurl urlwithstring:postvalues]]; nsdictionary *location = [response objectforkey:@"location"]; //array json data nsmutablearray *jsondata = [[nsmutablearray alloc] init]; (nsdictionary *dict in location) { bathroom *bathroom = [[[bathroom alloc] init] autorelease]; bathroom.name = [dict objectforkey:@"name"]; bathroom.street = [dict objectforkey:@"address"]; bathroom.city = [dict objectforkey:@"city"]; bathroom.state = [dict objectforkey:@"state"]; bathroom.postal = [dict objectforkey:@"postalcode"]; bathroom.country = [dict objectforkey:@"country"]; [jsondata addobject:bathroom]; } try updating following:
//get server response id response = [self objectwithurl:[nsurl urlwithstring:postvalues]]; nsarray *venues = [[response objectforkey:@"response"] objectforkey:@"venues"]; //array json data nsmutablearray *jsondata = [[nsmutablearray alloc] init]; (nsdictionary *dict in venues) { bathroom *bathroom = [[[bathroom alloc] init] autorelease]; nsdictionary *location = [dict objectforkey:@"location"]; bathroom.name = [location objectforkey:@"name"]; bathroom.street = [location objectforkey:@"address"]; bathroom.city = [location objectforkey:@"city"]; bathroom.state = [location objectforkey:@"state"]; bathroom.postal = [location objectforkey:@"postalcode"]; bathroom.country = [location objectforkey:@"country"]; [jsondata addobject:bathroom]; }
Comments
Post a Comment