如何从 Google 个位置 api 解析 json 视口?
How parse json viewport from Google Places api?
通过我的 ios 应用程序,我向 google 个地方 api 发出了请求,我得到了这样的回复:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Rome",
"short_name" : "Rome",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Rome",
"short_name" : "Rome",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Metropolitan City of Rome",
"short_name" : "RM",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Lazio",
"short_name" : "Lazio",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italy",
"short_name" : "IT",
"types" : [ "country", "political" ]
}
],
"adr_address" : "\u003cspan class=\"locality\"\u003eRome\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eItaly\u003c/span\u003e",
"formatted_address" : "Rome, Italy",
"geometry" : {
"location" : {
"lat" : 41.9027835,
"lng" : 12.4963655
},
"viewport" : {
"northeast" : {
"lat" : 42.0505462,
"lng" : 12.7302888
},
"southwest" : {
"lat" : 41.769596,
"lng" : 12.341707
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "c201ff6d6339dac3b34184b3972b232aa097ff8a",
"name" : "Rome",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"reference" : "CnRoAAAAUe_x9QmJ7kGAAAoyOwa_6vGISj0hy4mvqTJNjNl9TrXqaowyKQCEQov70GTyVidSdNd9wy0MG9UWffjSmi58YG7R3j2Fr9_RoKJCjKgcxwijojmVgFNf5p-8Ja1E53D_YnzW8R0lgtY1xMmOZvxzEBIQ1ruuMP92aFYZs-EJd0McJRoUOaLCDU0K4Dh9nag9wouUAsHqC3g",
"scope" : "GOOGLE",
"types" : [ "locality", "political" ],
"url" : "https://maps.google.com/maps/place?q=Rome,+Italy&ftid=0x132f6196f9928ebb:0xb90f770693656e38",
"vicinity" : "Rome"
},
"status" : "OK"
}
我用这个代码得到 address_components:
NSDictionary *ResultDictionary = [body objectFromJSONString];
for (NSDictionary *q in [[ResultDictionary valueForKey:@"result"]valueForKey:@"address_components"]) {
NSArray *type=[q valueForKey:@"types"];
NSArray *long_name=[q valueForKey:@"long_name"];
NSArray *short_name=[q valueForKey:@"short_name"];
}
使用相同的逻辑我无法获取几何对象下的视口和位置值,这会导致崩溃,因为密钥不存在:
for (NSDictionary *q in [[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] ) {
NSArray *location=[q valueForKey:@"location"];
}
似乎 json 解码只提取了一个字符串,而不是几何对象的 nsDictionary,有什么问题吗?
你的 "geometry" 节点包含两个字典,其中只有一个有 "location" 节点,这就是它崩溃的原因(你要求他们都返回 key [=16 的值=] 而不是先检查是否有)。提供的答案将解决您的问题,只需直接钻取 "location" 部分而不进行迭代,您将获得您的值。
考虑到您已经得到了答案,您可能会通过使用更高级别的 JSON 库来避免一些麻烦,例如这个库:http://www.jsonmodel.com/
将此添加到您的代码中并根据您的要求从 JSON
中获取您的价值
NSMutableDictionary *NewDict=[[[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] objectForKey:@"location"];
NSString *lat=[NewDict objectForKey:@"lat"];
NSString *lng =[NewDict objectForKey:@"lng"];
希望这段代码对你有用。
通过我的 ios 应用程序,我向 google 个地方 api 发出了请求,我得到了这样的回复:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Rome",
"short_name" : "Rome",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Rome",
"short_name" : "Rome",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Metropolitan City of Rome",
"short_name" : "RM",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Lazio",
"short_name" : "Lazio",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italy",
"short_name" : "IT",
"types" : [ "country", "political" ]
}
],
"adr_address" : "\u003cspan class=\"locality\"\u003eRome\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eItaly\u003c/span\u003e",
"formatted_address" : "Rome, Italy",
"geometry" : {
"location" : {
"lat" : 41.9027835,
"lng" : 12.4963655
},
"viewport" : {
"northeast" : {
"lat" : 42.0505462,
"lng" : 12.7302888
},
"southwest" : {
"lat" : 41.769596,
"lng" : 12.341707
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "c201ff6d6339dac3b34184b3972b232aa097ff8a",
"name" : "Rome",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"reference" : "CnRoAAAAUe_x9QmJ7kGAAAoyOwa_6vGISj0hy4mvqTJNjNl9TrXqaowyKQCEQov70GTyVidSdNd9wy0MG9UWffjSmi58YG7R3j2Fr9_RoKJCjKgcxwijojmVgFNf5p-8Ja1E53D_YnzW8R0lgtY1xMmOZvxzEBIQ1ruuMP92aFYZs-EJd0McJRoUOaLCDU0K4Dh9nag9wouUAsHqC3g",
"scope" : "GOOGLE",
"types" : [ "locality", "political" ],
"url" : "https://maps.google.com/maps/place?q=Rome,+Italy&ftid=0x132f6196f9928ebb:0xb90f770693656e38",
"vicinity" : "Rome"
},
"status" : "OK"
}
我用这个代码得到 address_components:
NSDictionary *ResultDictionary = [body objectFromJSONString];
for (NSDictionary *q in [[ResultDictionary valueForKey:@"result"]valueForKey:@"address_components"]) {
NSArray *type=[q valueForKey:@"types"];
NSArray *long_name=[q valueForKey:@"long_name"];
NSArray *short_name=[q valueForKey:@"short_name"];
}
使用相同的逻辑我无法获取几何对象下的视口和位置值,这会导致崩溃,因为密钥不存在:
for (NSDictionary *q in [[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] ) {
NSArray *location=[q valueForKey:@"location"];
}
似乎 json 解码只提取了一个字符串,而不是几何对象的 nsDictionary,有什么问题吗?
你的 "geometry" 节点包含两个字典,其中只有一个有 "location" 节点,这就是它崩溃的原因(你要求他们都返回 key [=16 的值=] 而不是先检查是否有)。提供的答案将解决您的问题,只需直接钻取 "location" 部分而不进行迭代,您将获得您的值。
考虑到您已经得到了答案,您可能会通过使用更高级别的 JSON 库来避免一些麻烦,例如这个库:http://www.jsonmodel.com/
将此添加到您的代码中并根据您的要求从 JSON
中获取您的价值
NSMutableDictionary *NewDict=[[[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] objectForKey:@"location"];
NSString *lat=[NewDict objectForKey:@"lat"];
NSString *lng =[NewDict objectForKey:@"lng"];
希望这段代码对你有用。