2013-10-21 34 views
1

我是新來的ios和分析工作,並得到了如下圖所示的網址: -解析API

@"https://maps.googleapis.com/maps/api/place/textsearch/xml?query=%@&sensor=true&key=%@",typedtext,kGOOGLE_API_KEY 

,但我不能夠解析它。

我對此不知情,應該怎麼做。

所以,請請幫助我。

+0

鏈接返回什麼? –

+0

此鏈接將返回地圖視圖上的地點。 –

+0

它返回的XML或JSON? – Indra

回答

0

如果該鏈接返回json,則可以將響應放入NSData中,並使用JSONObjectWothData:options:error:解析該響應。

EX。

NSString *urlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false 
"]; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData* data = [NSData dataWithContentsOfURL: 
         [NSURL URLWithString:urlPercoso]]; 

    json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error]; 
}); 

凡JSON是一個NSDictionary

然後您會收到一個結構類似:

{ 
    "results" : [ 
     { 
     "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" : "Santa Clara", 
       "short_name" : "Santa Clara", 
       "types" : [ "administrative_area_level_2", "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" ] 
      } 
     ], 
     "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", 
     "geometry" : { 
      "location" : { 
       "lat" : 37.42291810, 
       "lng" : -122.08542120 
      }, 
      "location_type" : "ROOFTOP", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 37.42426708029149, 
        "lng" : -122.0840722197085 
       }, 
       "southwest" : { 
        "lat" : 37.42156911970850, 
        "lng" : -122.0867701802915 
       } 
      } 
     }, 
     "types" : [ "street_address" ] 
     } 
    ], 
    "status" : "OK" 
} 

這麼喜歡數據可以採取人的信息,因爲現在只是一個字典,數組,字符串和數字值,那麼你可以做到這一點,只爲更多的信息看看這個link

+0

thanx的幫助,但你可以請你幫我與示例代碼.... –

+0

你試過我的代碼? –

+0

w8我jst嘗試.... 5分鐘... –