1

I just successfully finished making my first JSON request and deserializing the information now all I need to do is know how to gather a few values from my dictionary. Here's what the request looks like:

@implementation ViewController {
    GMSMapView *mapView_;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Make the URL connection in order to download JSON/XML data.
    NSString *urlAsString = @"weburlgoeshere...";
    NSURL *url = [NSURL URLWithString:urlAsString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // Error and success message handling.
    [NSURLConnection
    sendAsynchronousRequest:urlRequest
    queue:queue
     completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
         if([data length] > 0 &&
            error == nil){
             NSData *jsonData = [NSData dataWithContentsOfURL:url];

             if (jsonData != nil){
                 NSError *error = nil;

                 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
                 if(error == nil)
                 NSLog(@"%@", [result valueForKey:@"merchants"]);
             }

         }
         else if ([data length] == 0 &&
         error == nil){
         NSLog(@"Nothing was downloaded");

         }
         else if (error != nil){
         NSLog(@"Error happened = %@", error);

         }


     }];

}

This is what logs out to the console:

branches =         (
                        {
                city = "......";
                country = "United States";
                countryIsoCode = USA;
                distanceInKms = "8.31";
                distanceInMiles = "5.16";
                id = 7952205;
                latitude = ".......";
                longitude = "........";
                name = ".......";
                state = ......;
                stateIsoCode = .....;
                street = ".........";
                telephone = "";
            }
        );
        id = 174535;
        logoUrl = ".......";
        name = ".......";

So I stored all these values in an NSDictionary that I called "result" and I would like to know how to gather and store SPECIFIC key values for latitude and longitude in an NSNumber. I'm thinking that I might need to fastEnumerate most of these with a block, and then deal with the accordingly. The intent here is to use these values and display them onto a map. Any help would be greatly appreciated! Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

You can access Required values for your keys in this manner.

NSArray * array = [dic objectForKey:@"branches"];
NSDictionary *furtherNames = array[0];
NSNumber *latitude = [NSNumber numberWithFloat:[[furtherNames objectForKey:@"latitude"] floatValue]];
NSNumber *longitude = [NSNumber numberWithFloat:[[furtherNames objectForKey:@"longitude"] floatValue]];

All you need to observe is that () => Array of objects, {} => Dictionary. Hence traverse them accordingly.

Hope this helps.

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41
  • Okay, I just realized that I could just create a @property in order to store the values in the dictionary. I think what's really strange is that one of those keys (latitude and longitude) are coming up when I call the following: [dic allKeys]. Maybe it's because they're nested? Any ideas? – user2745444 Sep 04 '13 at 10:47
  • +1, and you can use newer syntax result[@"branches"][0][@"latitude"]. @user2745444, you won't see nested keys if you log allKeys, so your dictionary (dic) must be pointing to one of the nested dictionaries. – danh Sep 04 '13 at 16:43
  • That worked perfectly! I was able to grab the exact value that I needed! Would I have to use a fastEnumeration block in order to create an array of all of these values individually? Thanks! – user2745444 Sep 04 '13 at 17:36