1

我是 ios 新手,正在解析并得到如下所示的 url:-

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

但我无法解析它。

我对此一无所知,应该如何做。

所以请帮助我。

4

2 回答 2

0

如果该链接返回一个 json,您可以将响应放在 a 中NSData,而不是JSONObjectWothData:options:error:解析它。

前任。

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"
}

所以像数据一样,您可以获取所有信息,因为现在只是一个字典、数组、字符串和数值,所以您可以单独执行此操作以获取更多信息,请查看此链接

于 2013-10-21T10:52:20.517 回答
0

请按照它的工作代码进行 xml 解析

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

于 2013-10-21T11:07:34.843 回答