How to parse specific googlemaps API webservices JSON repsonse

The google Maps API JSON response return different parameters with the same noun:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

The parameter lat and lng exists multiple times in the response, for example, assuming that i need to get the lat/lng of the location parameter:

  • Fastest YUV420P to RGBA conversion on iOS using the CPU
  • Load more options with UITableView
  • UITextField format in xx-xx-xxx
  • iOS: How to copy HTML into the cut-paste buffer?
  • Iphone: Is it possible to hide the TabBar? (Pre-iOS 8)
  • Rotate GMSMarker in direction at which user facing
  • "location": {
                "lat": 37.4219720,
                "lng": -122.0841430
              },
    

    What should i do for my JSON parsing code:

    NSDictionary *responseDict = [responseString JSONValue];
        double latitude = [responseDict objectForKey:@"lat"];
        double longitude = [responseDict objectForKey:@"lng"];
    

    This is what i write, how does the parser know if i mean explicitly the lat/lng of the location parameter or another one?

    3 Solutions Collect From Internet About “How to parse specific googlemaps API webservices JSON repsonse”

    To make sure you get the right lat and lon values that you are after, you would need to get the results array, then the results dictionary, then the geometry dictionary, followed by the location dictionary, like so:

    NSDictionary *responseDict = [responseString JSONValue];
    
    // The "results" object is an array that contains a single dictionary: 
    // "results": [{...}]
    // So first lets get the results array
    NSArray *resultsArray = [responseDict objectForKey:@"results"];
    
    // Then get the results dictionary
    NSDictionary *resultsDict = [resultsArray objectAtIndex:0];
    
    // Once we have the results dictionary, we can get the geometry
    NSDictionary *geometryDict = [resultsDict objectForKey:@"geometry"];
    
    // Then we get the location
    NSDictionary *locationDict = [geometryDict objectForKey:@"location"];
    
    // Now we can get the latitude and longitude
    double latitude = [locationDict objectForKey:@"lat"];
    double longitude = [locationDict objectForKey:@"lng"];
    

    It may not be necessary to mention but for the beginner’s sake, the last two line should be:

    double latitude = [[locationDict objectForKey:@"lat"] doubleValue];
    double longitude = [[locationDict objectForKey:@"lng"] doubleValue];
    

    This function will give you expected result

    - (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
    {
      double latitude = 0, longitude = 0;
      NSString *straddr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", straddr];
      NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
     if (result) 
     {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil])
        {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil])
            {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    
    return center;
    
    }
    

    Hope this will help someone.