gpt4 book ai didi

来自谷歌解析json的iOS地理编码数据

转载 作者:行者123 更新时间:2023-12-02 07:51:36 25 4
gpt4 key购买 nike

我正在从谷歌地图中提取一些数据,但我似乎无能为力。这是我的代码:

- (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:

结果 = (
{
“地址组件”=(
{
“长名称” = 2900;
“短名称” = 2900;
类型 = (
“街牌号码”
);
},
{
"long_name"= "地平线博士";
"short_name"= "地平线博士";
类型 = (
路线
);
},
{
"long_name"= "普鲁士国王";
"short_name"= "普鲁士国王";
类型 = (
地方,
政治的
);
},
{
"long_name"= "Upper Merion";
"short_name"= "Upper Merion";
类型 = (
“管理区域_级别_3”,
政治的
);
},
{
“long_name”=蒙哥马利;
"short_name"= 蒙哥马利;
类型 = (
“管理区域_级别_2”,
政治的
);
},
{
“long_name”=宾夕法尼亚;
“短名称” = PA;
类型 = (
“管理区域_级别_1”,
政治的
);
},
{
"long_name"= "美国";
"short_name"= 美国;
类型 = (
国家,
政治的
);
},
{
“长名称” = 19406;
“短名称” = 19406;
类型 = (
“邮政编码”
);
}
);
"formatted_address"= "2900 Horizo​​n Dr, King of Prussia, PA 19406, USA";
几何= {
位置 = {
纬度 = "40.0896985";
lng = "-75.341717";
};
“location_type”=屋顶;
视口(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com