簡體   English   中英

如何從Google Places API解析JSON視口?

[英]How parse json viewport from Google Places api?

使用我的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\"\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,這是怎么回事?

將其添加到您的代碼中,並根據需要從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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM