Ios JSON框架解析时出错
我在解析来自GooglePlaces的JSON时遇到问题 我有以下代码来解析它:Ios JSON框架解析时出错,ios,json,json-framework,Ios,Json,Json Framework,我在解析来自GooglePlaces的JSON时遇到问题 我有以下代码来解析它: NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=bank&sen
NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=bank&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
if ([myRawJson length] == 0) {
[myRawJson release];
return;
}
SBJSON *parser = [[SBJSON alloc] init];
list = [[parser objectWithString:myRawJson error:nil] copy];
NSDictionary *results = [myRawJson JSONValue];
placesLatitudes = [[NSMutableArray alloc] init];
NSDictionary *latslngs = [[results objectForKey:@"results"] valueForKey:@"location"];
NSArray *teste = [latslngs objectForKey:@"location"];
for (NSDictionary *element in teste)
{
NSString *latitude = [element objectForKey:@"lat"];
NSLog(@"%@", latitude);
}
[parser release];
URL请求返回以下JSON:
{
"html_attributions" : [ "Listagens por \u003ca href=\"http://www.telelistas.net/\"\u003eTelelistas\u003c/a\u003e" ],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -15.8192680,
"lng" : -47.9146680
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "1a01f2887f1a70bd430d6a7ffa2a7e46974a1cb7",
"name" : "Banco Bradesco S/A",
"reference" : "CnRqAAAAXr_6FCDMlpeVF-E0b3cDxNFGzmS1bYBBGc4v4lKrcusGQPEnx1MXnCJVb3nCVWalu2IOwN9oSVtcXS6_W8JLL_CMhKzkm75UqGt5ShX_s0d4coxOBYsbo66JP1NpF9c5Ua7OxyjepQferD6SbAIjIhIQZ6qcgQT8hqAXtFTuPzuZThoUhrCcxKMRwZq2vl8Sv8LJes7d-no",
"types" : [ "bank", "finance", "establishment" ],
"vicinity" : "CRS 511 Bl B s/n lj 15/21 - Brasília"
},
{
"geometry" : {
"location" : {
"lat" : -15.8177890,
"lng" : -47.9126370
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "e2c56c8ca6643e0f0cef6bd0405fd8c6a30850ad",
"name" : "UNIBANCO-União de Bancos Brasileiros S/A",
"reference" : "CpQBgQAAABkp74S1WF0LfZEhkNj6TUbmiPu2djL81IDnFMJhRR2HDx7336PlRh46q16FwCao290T1smo1wNsGQ-sRVZ_S-MClYUDQzdpaTdNVty0JHBjQTEOMVo0yW8Uzd_OcuI12v8eZ81wu5V7sgHomBw-SeE-mhrPntOU1EzmOANNhIDRExdKmrof2hIKHdLJJaccdRIQeQ9uN-L66Ztmz4_2dkk7DhoUZxcYaZqXgXXnAwWB97e6bNCNp8A",
"types" : [ "bank", "finance", "establishment" ],
"vicinity" : "Bl CRS 510 Bl A s/n - Brasília"
}
],
"status" : "OK"
}
我正在尝试获取路径结果>几何体>位置>lat中的信息,但使用上面的代码,我得到错误:-[\uu NSArrayI objectForKey:]:无法识别的选择器发送到实例0x5a526e0
有人知道如何使用iOS的JSON框架解析这些数据吗?
谢谢 结果是一个数组,而不是一个字典。位置是字典,而不是数组
NSDictionary *results = [myRawJson JSONValue];
foreach (NSDictionary *result in [results objectForKey:@"results"])
{
NSDictionary *location = [[result objectForKey:@"geometry"] objectForKey:@"location"];
NSString *latitude = [location objectForKey:@"lat"];
NSString *longitude = [location objectForKey:@"lng"];
}
我没有测试编译那段代码,但应该很接近。结果是一个数组,而不是一个字典。位置是字典,而不是数组
NSDictionary *results = [myRawJson JSONValue];
foreach (NSDictionary *result in [results objectForKey:@"results"])
{
NSDictionary *location = [[result objectForKey:@"geometry"] objectForKey:@"location"];
NSString *latitude = [location objectForKey:@"lat"];
NSString *longitude = [location objectForKey:@"lng"];
}
Here Parse Data From Google Map API
-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true",pdblLatitude, pdblLongitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
// NSLog(@"%@",locationString);
locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSURL *requestURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];
//response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
if ([locationResults count]==0) {
[AQHelper showAlert:self withTitle:@"Empty Location" withMessage:@"Location Not Found!"];
return nil;
}
else{
NSString *fullAddress = [[[locationResults objectForKey:@"results"] objectAtIndex:0] objectForKey:@"formatted_address"]
;
self.address=fullAddress;
id allValues = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray *result = [(NSDictionary*)allValues objectForKey:@"results"];
// for(int i=0;i<[result count];i++)
// {
NSDictionary *values = (NSDictionary*)[result objectAtIndex:0];
NSArray *component = [(NSDictionary*)values objectForKey:@"address_components"];
for(int j=0;j<[component count];j++)
{
NSDictionary *parts = (NSDictionary*)[component objectAtIndex:j];
if([[parts objectForKey:@"types"] containsObject:@"route"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.street=[parts objectForKey:@"long_name"];
}
else{
self.street=[parts objectForKey:@"short_name"];
}
}
if([[parts objectForKey:@"types"] containsObject:@"locality"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.city=[parts objectForKey:@"long_name"];
}
else{
self.city=[parts objectForKey:@"short_name"];
}
}
if([[parts objectForKey:@"types"] containsObject:@"administrative_area_level_2"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.state=[parts objectForKey:@"long_name"];
}
else{
self.state=[parts objectForKey:@"short_name"];
}
}
if([[parts objectForKey:@"types"] containsObject:@"postal_code"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.zip=[parts objectForKey:@"long_name"];
}
else{
self.zip=[parts objectForKey:@"short_name"];
}
}
}
}
return self.address;
}
我没有测试编译该代码,但应该很接近了。这里解析来自谷歌地图API的数据
Here Parse Data From Google Map API
-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true",pdblLatitude, pdblLongitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
// NSLog(@"%@",locationString);
locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSURL *requestURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];
//response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
if ([locationResults count]==0) {
[AQHelper showAlert:self withTitle:@"Empty Location" withMessage:@"Location Not Found!"];
return nil;
}
else{
NSString *fullAddress = [[[locationResults objectForKey:@"results"] objectAtIndex:0] objectForKey:@"formatted_address"]
;
self.address=fullAddress;
id allValues = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray *result = [(NSDictionary*)allValues objectForKey:@"results"];
// for(int i=0;i<[result count];i++)
// {
NSDictionary *values = (NSDictionary*)[result objectAtIndex:0];
NSArray *component = [(NSDictionary*)values objectForKey:@"address_components"];
for(int j=0;j<[component count];j++)
{
NSDictionary *parts = (NSDictionary*)[component objectAtIndex:j];
if([[parts objectForKey:@"types"] containsObject:@"route"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.street=[parts objectForKey:@"long_name"];
}
else{
self.street=[parts objectForKey:@"short_name"];
}
}
if([[parts objectForKey:@"types"] containsObject:@"locality"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.city=[parts objectForKey:@"long_name"];
}
else{
self.city=[parts objectForKey:@"short_name"];
}
}
if([[parts objectForKey:@"types"] containsObject:@"administrative_area_level_2"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.state=[parts objectForKey:@"long_name"];
}
else{
self.state=[parts objectForKey:@"short_name"];
}
}
if([[parts objectForKey:@"types"] containsObject:@"postal_code"])
{
if ([parts objectForKey:@"long_name"]!=0) {
self.zip=[parts objectForKey:@"long_name"];
}
else{
self.zip=[parts objectForKey:@"short_name"];
}
}
}
}
return self.address;
}
-(NSString*)getAddressFromLatLon:(双)PDBLlations with longitude:(双)PDBLlations
{
NSString*urlString=[NSString stringWithFormat:@]http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true”,PDBLlation,pdblLongitude];
n错误*错误;
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]编码:NSASCISTRING编码错误:&error];
//NSLog(@“%@”,locationString);
locationString=[locationString StringByReplacingOfString:@“\”和String:@”“]的发生率;
NSURL*requestURL=[NSURL URLWithString:urlString];
NSURLRequest*request=[NSURLRequest requestWithURL:(requestURL)];
//回应
NSData*响应=[NSURLConnection sendSynchronousRequest:request returningResponse:nil错误:nil];
N错误*jsonParsingError=nil;
NSDictionary*locationResults=[NSJSONSerialization JSONObjectWithData:响应选项:0错误:&jsonParsingError];
如果([locationResults count]==0){
[AQHelper showAlert:self with title:@“空位置”with message:@“未找到位置!”;
返回零;
}
否则{
NSString*fullAddress=[[locationResults objectForKey:@“结果”]objectAtIndex:0]objectForKey:@“格式化的_地址”]
;
self.address=完整地址;
id allValues=[NSJSONSerialization JSONObject WithData:响应选项:0错误:&jsonParsingError];
NSArray*结果=[(NSDictionary*)所有值objectForKey:@“结果”];
//对于(inti=0;i这里解析来自GoogleMapAPI的数据
-(NSString*)getAddressFromLatLon:(双)PDBLlations with longitude:(双)PDBLlations
{
NSString*urlString=[NSString stringWithFormat:@]http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true”,PDBLlation,pdblLongitude];
n错误*错误;
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]编码:NSASCISTRING编码错误:&error];
//NSLog(@“%@”,locationString);
locationString=[locationString StringByReplacingOfString:@“\”和String:@”“]的发生率;
NSURL*requestURL=[NSURL URLWithString:urlString];
NSURLRequest*request=[NSURLRequest requestWithURL:(requestURL)];
//回应
NSData*响应=[NSURLConnection sendSynchronousRequest:request returningResponse:nil错误:nil];
N错误*jsonParsingError=nil;
NSDictionary*locationResults=[NSJSONSerialization JSONObjectWithData:响应选项:0错误:&jsonParsingError];
如果([locationResults count]==0){
[AQHelper showAlert:self with title:@“空位置”with message:@“未找到位置!”;
返回零;
}
否则{
NSString*fullAddress=[[locationResults objectForKey:@“结果”]objectAtIndex:0]objectForKey:@“格式化的_地址”]
;
self.address=完整地址;
id allValues=[NSJSONSerialization JSONObject WithData:响应选项:0错误:&jsonParsingError];
NSArray*结果=[(NSDictionary*)所有值objectForKey:@“结果”];
//为了(int i=0;i需要注意的另一件事是location
是一个字典,因此必须使用快速枚举或allKeys
数组进行枚举。如果需要枚举,则为True,但在本例中并非如此。另一件需要注意的事是location
是一个字典,因此必须使用快速枚举进行枚举如果需要枚举,则为True,但在本例中不是这样。