如何解析特定的谷歌地图API Web服务JSON响应

时间:2011-12-26 10:19:18

标签: ios json google-maps

Google Maps API JSON响应使用相同的名词返回不同的参数:

{
  "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
        }
      }
    }
  } ]
}

参数latlng在响应中多次出现,例如,假设我需要获取位置参数的lat/lng

"location": {
            "lat": 37.4219720,
            "lng": -122.0841430
          },

我应该如何处理我的JSON解析代码:

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

这就是我写的,解析器如何知道我是否明确表示位置参数的lat / lng还是另一个?

3 个答案:

答案 0 :(得分:3)

为了确保获得正确的lat和lon值,您需要获取results数组,然后是results字典,然后是geometry字典,然后是location字典,如下所示:

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

答案 1 :(得分:2)

可能没有必要提及但是为了初学者的缘故,最后两行应该是:

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

答案 2 :(得分:0)

此功能将为您提供预期的结果

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

}

希望这会对某人有所帮助。