我正在从谷歌 map 中提取一些数据,但我似乎无法对它做任何事情。这是我的代码:

- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection  {

//do something with the data!
NSError *e = nil;
//parse the json data
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];

//get the lat and long and put it into an array
locationData = [[NSMutableArray alloc] init];

NSLog(@"%@", [jsonArray objectAtIndex:0]);

}

如果我记录 jsonArray.count 我得到 2,这似乎是正确的,因为谷歌将返回顶级的结果和状态。但是,如果我尝试获取对象 0,它就会崩溃。如果我尝试做这样的事情它也会崩溃:
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection  {

//do something with the data!
NSError *e = nil;
//parse the json data
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];

//get the lat and long and put it into an array
locationData = [[NSMutableArray alloc] init];

for(NSDictionary* thisLocationDict in jsonArray) {

    NSString* location = [thisLocationDict objectForKey:@"results"];
    [locationData addObject:location];
}
}

我使用此代码从 Twitter 获取数据,没有任何问题。我在控制台中得到的错误是我试图获取一个字符串对象:
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x798a270

这是谷歌发送给我的json:

结果 = (
{
“地址_组件”=(
{
“long_name”= 2900;
“short_name”= 2900;
类型 = (
“街道号码”
);
},
{
"long_name"= "地平线博士";
"short_name"= "地平线博士";
类型 = (
路线
);
},
{
"long_name"= "普鲁士国王";
"short_name"= "普鲁士国王";
类型 = (
地方,
政治的
);
},
{
"long_name"= "Upper Merion";
"short_name"= "Upper Merion";
类型 = (
"administrative_area_level_3",
政治的
);
},
{
“long_name” = 蒙哥马利;
“short_name” = 蒙哥马利;
类型 = (
"administrative_area_level_2",
政治的
);
},
{
“long_name” = 宾夕法尼亚州;
“short_name”= PA;
类型 = (
"administrative_area_level_1",
政治的
);
},
{
"long_name"= "美国";
“short_name” = 美国;
类型 = (
国家,
政治的
);
},
{
“long_name”= 19406;
“short_name” = 19406;
类型 = (
“邮政编码”
);
}
);
"formatted_address"= "2900 Horizo​​n Dr, King of Prussia, PA 19406, USA";
几何 = {
位置 = {
纬度=“40.0896985”;
lng = "-75.341717";
};
“位置类型”=屋顶;
视口(viewport) = {
东北 = {
纬度=“40.09104748029149”;
lng = "-75.34036801970849";
};
西南 = {
纬度=“40.0883495197085”;
lng = "-75.34306598029151";
};
};
};
类型 = (
“街道地址”
);
}
);
状态 = 正常;
}

和我传递的网址:
http://maps.googleapis.com/maps/api/geocode/json?address=2900+Horizon+Drive+King+of+Prussia+,+PA&sensor=false

知道我做错了什么吗?

最佳答案

与您从 google 获得的数据的不同之处在于它由键分开,例如 results, geometry, formatted_address ...

你应该做这样的事情:

NSError *error;

NSString *lookUpString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&components=country:AU&sensor=false", self.searchBar.text];

lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];

self.locationArray = [[jsonDict valueForKey:@"results"] valueForKey:@"formatted_address"];

int total = self.locationArray.count;
NSLog(@"locationArray count: %d", self.locationArray.count);

for (int i = 0; i < total; i++)
{
    NSString *statusString = [jsonDict valueForKey:@"status"];
    NSLog(@"JSON Response Status:%@", statusString);

    NSLog(@"Address: %@", [self.locationArray objectAtIndex:i]);

}

关于来自谷歌解析json的iOS地理编码数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10955693/

10-13 07:43