如何在objective-c中解析来自google的地理位置json以获取lng和lat

发布于 2024-10-13 08:28:32 字数 1303 浏览 2 评论 0原文

我正在创建一个网络请求,以根据 zip 提取 lng 和 lat 数据(使用以下网址)

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

这样做我得到以下 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 ]
    }
  } ]
}

我试图从中提取的只是坐标部分。这是我当前的尝试,当我点击下面所示的最后一行时,会抛出异常。

  //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"];

为了快速提取坐标,我在这里缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2024-10-20 08:28:32

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

NSDictionary * json = [string JSONValue];

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

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

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

NSDictionary *firstPlacemark = [placemarks objectAtIndex:0];

在该字典中,您需要键“Point”下的字典:

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

在该字典中,您需要键“cocodices”下的数组":

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

此时,您拥有包含 3 个 NSNumber 对象的数组。瞧!


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

NSArray * coordinates = [json valueForKeyPath:@"Placemark[0].Point.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! :)

蓝海似她心 2024-10-20 08:28:32

请记住,“[ -93.7353858, 41.5998115, 0 ]”不是字符串

Keep in mind that "[ -93.7353858, 41.5998115, 0 ]" is not a string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文