【发布时间】:2011-12-26 10:19:18
【问题描述】:
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
}
}
}
} ]
}
参数lat和lng在响应中存在多次,例如假设我需要获取location参数的lat/lng:
"location": {
"lat": 37.4219720,
"lng": -122.0841430
},
我的 JSON 解析代码应该怎么做:
NSDictionary *responseDict = [responseString JSONValue];
double latitude = [responseDict objectForKey:@"lat"];
double longitude = [responseDict objectForKey:@"lng"];
这是我写的,解析器如何知道我是明确表示位置参数的纬度/经度还是另一个?
【问题讨论】:
标签: ios json google-maps