如何解析从谷歌地理定位json在lng和lat中的objective-c [英] How to parse the geolocation json from google for lng and lat in objective-c

查看:163
本文介绍了如何解析从谷歌地理定位json在lng和lat中的objective-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个web请求,以根据zip拉动lng和lat数据(使用以下url):

http://maps.google.com/maps/geo?q=50266



这样做我回来了以下json

  {
name:50266,
状态:{
code:200,
request:geocode
},
地标:[{
id:p1,
地址:美国爱达荷州西德梅因,IA 50266,
AddressDetails:{
准确性:5,
Country:{
AdministrativeArea:{
AdministrativeAreaName:IA,
Locality:{
LocalityName:West Des Moines,
PostalCode:{
PostalCodeNumber:50266
}
}
},
CountryName:USA,
CountryNameCode:US
}
},
ExtendedData:{
LatLonBox:{
north:41.6005010,
south:41.5254700,
east:-93.7347000,
west:-93.8435030


Point:{
坐标:[-93.7353858,41,5998115,0]
}
}]
}

我试图从中得到的仅仅是坐标部分。这是我当前尝试抛出和异常,当我击中下面显示的最后一行

  //它到JSON和这是工作
NSArray * json = [items JSONValue];

NSString * coords = [json objectForKey:@Placemark.Point.coordinates];

我在这里错过了快速拉动的坐标吗?

解决方案

总体返回值不是数组,它是一个字典。请注意,第一个字符是 {。如果它是一个数组,它将是 [

  NSDictionary * json = [string JSONValue]; 

现在,您需要地标键下的内容。请注意,这会返回一个数组,因为Placemarks键(和冒号)后面的字符是 [

  NSArray * placemarks = [json objectForKey:@Placemark]; 

在这个数组中,您需要第一个元素,它是另一个 NSDictionary

  NSDictionary * firstPlacemark = [placemarks objectAtIndex:0]; 

在这本词典中,您希望字典在Point键下:

  NSDictionary * point = [firstPlacemark objectForKey:@Point]; 

从这本词典中,您需要在坐标键下的数组:

  NSArray * coordinates = [point objectForKey:@coordinates]; 

此时,您的数组包含3 NSNumber 对象。 Voilá!




对于高级用户,您可以使用键值编码来获取它:

  NSArray * coordinates = [json valueForKeyPath:@Placemark [0] .Point.coordinates]; 

虽然我不会建议您,除非您清楚了解发生了什么。



这是行不通的。没关系! :)

I'm creating a web request to pull lng and lat data based on zip (using the following url)

http://maps.google.com/maps/geo?q=50266

Doing so I get back the following json

{
  "name": "50266",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "West Des Moines, IA 50266, USA",
    "AddressDetails": {
   "Accuracy" : 5,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "IA",
         "Locality" : {
            "LocalityName" : "West Des Moines",
            "PostalCode" : {
               "PostalCodeNumber" : "50266"
            }
         }
      },
      "CountryName" : "USA",
      "CountryNameCode" : "US"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 41.6005010,
        "south": 41.5254700,
        "east": -93.7347000,
        "west": -93.8435030
      }
    },
    "Point": {
      "coordinates": [ -93.7353858, 41.5998115, 0 ]
    }
  } ]
}

What I'm trying to pull from this is simply the coordinates section. Here is my current attempt that throws and exception when I hit the last line shown below

  //after I get the response I turn it into json and this is working
  NSArray* json = [items JSONValue];

  NSString* coords = [json objectForKey:@"Placemark.Point.coordinates"];

What I'm I missing here for a quick pull of the coordinates?

解决方案

The overall value return is not an array, it's a dictionary. Note that the first character is {. If it were an array, it would be [.

NSDictionary * json = [string JSONValue];

Now, you want the stuff under the "Placemarks" key. Note that this returns an array, since the character after the "Placemarks" key (and colon) is a [.

NSArray * placemarks = [json objectForKey:@"Placemark"];

From this array, you want the first element, which is another NSDictionary:

NSDictionary *firstPlacemark = [placemarks objectAtIndex:0];

From this dictionary, you want the dictionary under the key "Point":

NSDictionary *point = [firstPlacemark objectForKey:@"Point"];

From this dictionary, you want the array under the key "coordinates":

NSArray * coordinates = [point objectForKey:@"coordinates"];

At this point, you have the array that contains 3 NSNumber objects. Voilá!


For the advanced user, you can probably use key-value coding to get at it:

NSArray * coordinates = [json valueForKeyPath:@"Placemark[0].Point.coordinates"];

Though I wouldn't recommend that unless you clearly understand what's going on there.

That does not work. Never mind! :)

这篇关于如何解析从谷歌地理定位json在lng和lat中的objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆