如何从Google Places api解析json视口



使用我的ios应用程序,我向google places 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"u003eRomeu003c/spanu003e, u003cspan class="country-name"u003eItalyu003c/spanu003e",
  "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,怎么了?

将其添加到您的代码中,并根据您的要求从JSON获得您的价值

NSMutableDictionary *NewDict=[[[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] objectForKey:@"location"];
NSString *lat=[NewDict objectForKey:@"lat"];
NSString *lng =[NewDict objectForKey:@"lng"];

我希望这个代码对你有用。

您的"geometry"节点包含两个字典,其中只有一个字典具有"location"节点,这就是它崩溃的原因(您要求它们都返回键"location)的值,而不首先检查是否有)。提供的答案将解决您的问题,只需直接深入到"位置"部分,无需迭代,即可获得您的值。

考虑到您已经得到了答案,使用更高级别的JSON库(如以下库)可能会省去一些麻烦:http://www.jsonmodel.com/

相关内容

  • 没有找到相关文章

最新更新