解析谷歌apis

时间:2013-10-21 10:45:10

标签: ios iphone objective-c json parsing

我是ios的新手,正在解析并获得如下所示的网址: -

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

但我无法解析它。

我不知道这个,应该怎么做。

所以请帮助我。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

请遵循此xml解析的工作代码

https://github.com/71squared/TBXML