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!

4

1 回答 1

1

您可以通过这种方式访问​​密钥的必需值。

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]];

您需要注意的是 () => 对象数组,{} => 字典。因此相应地遍历它们。

希望这可以帮助。

于 2013-09-04T10:18:11.560 回答