如何在objective-c中解析来自google的地理位置json以获取lng和lat
我正在创建一个网络请求,以根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
总体值返回不是一个数组,而是一个字典。请注意,第一个字符是
{
。如果它是一个数组,它将是[
。现在,您需要“地标”键下的内容。请注意,这会返回一个数组,因为“Placemarks”键(和冒号)后面的字符是
[
。在此数组中,您需要第一个元素,它是另一个
NSDictionary
:在该字典中,您需要键“Point”下的字典:
在该字典中,您需要键“cocodices”下的数组":
此时,您拥有包含 3 个
NSNumber
对象的数组。瞧!对于高级用户,您可能可以使用键值编码来获取它:不过我不会推荐这样做,除非您清楚地了解那里发生了什么。不工作。没关系! :)
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[
.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
[
.From this array, you want the first element, which is another
NSDictionary
:From this dictionary, you want the dictionary under the key "Point":
From this dictionary, you want the array under the key "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:Though I wouldn't recommend that unless you clearly understand what's going on there.That does not work. Never mind! :)
请记住,“[ -93.7353858, 41.5998115, 0 ]”不是字符串
Keep in mind that "[ -93.7353858, 41.5998115, 0 ]" is not a string