您的位置:首页 > Web前端 > JavaScript

解析json串

2014-04-01 17:21 141 查看
假如要解析出json串中的lat和lng。可有以下两种方法,一种是层层解析,一种是直接取其根节点。

//一层一层的解析到lat节点

NSString *postString = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=true",searchBar.text];

NSURL *url
= [[NSURL alloc] initWithString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[self setRequest:[ASIHTTPRequest requestWithURL:url]];

[request startSynchronous];

NSError *error =[request error];

if(!error)

{

NSLog(@"====responseString====%@",[request responseString]);

}

else

{

NSLog(@"======faild======");

}

NSString *responseString =[request responseString];

NSMutableDictionary *value=[responseString JSONValue];

NSArray *results=[value objectForKey:@"results"];

NSLog(@"results::::::::::::::::%@",results);

NSString *status=[value objectForKey:@"status"];

NSLog(@"status:::::::::::::%@",status);

for(int i=0;i<[results count];i++)

{

NSArray *address_components=[[results objectAtIndex:i]objectForKey:@"address_components"];

NSLog(@"address_components:::::::::::%@",address_components);

for(int j=0;j<[address_components count];j++)

{

NSDictionary *long_name=[[address_components objectAtIndex:j]objectForKey:@"long_name"];

NSDictionary *short_name=[[address_components objectAtIndex:j]objectForKey:@"short_name"];

NSLog(@"long_name:::::::::::%@",long_name);

NSLog(@"short_name:::::::::::%@",short_name);

NSDictionary *type=[[address_components objectAtIndex:j] objectForKey:@"types"];

NSLog(@"types::::::::::%@",type);

}

NSString *formatted_address=[[results objectAtIndex:i] objectForKey:@"formatted_address"];

NSLog(@"formatted_address:::::::%@",formatted_address);

NSDictionary *geometry=[[results objectAtIndex:i]objectForKey:@"geometry"];

NSLog(@"geometry:::::::%@",geometry);

NSDictionary *partial_match=[[results objectAtIndex:i]objectForKey:@"partial_match"];

NSLog(@"partial_match::::::%@",partial_match);

NSDictionary *locatio=[geometry objectForKey:@"location"];

NSLog(@"locatio:::::::::%@",locatio);

float lat =[[locatio objectForKey:@"lat"] floatValue];

float lng =[[locatio objectForKey:@"lng"]floatValue];

NSLog(@"lat:::::::%f",lat);

NSLog(@"lng::::::::%f",lng);

NSString *location_type=[geometry objectForKey:@"location_type"];

NSLog(@"location_type::::::::%@",location_type);

NSDictionary *viewport=[geometry objectForKey:@"viewport"];

NSLog(@"viewport::::::::%@",viewport);

NSDictionary *northeast =[viewport objectForKey:@"northeast"];

NSLog(@"northeast:::::::::%@",northeast);

NSDictionary *southwest =[viewport objectForKey:@"southwest"];

NSLog(@"southwest:::::::::%@",southwest);

NSDictionary *types=[[results objectAtIndex:i]objectForKey:@"types"];

NSLog(@"types:::::::%@",types);

[self gotoLocation:lat and:lng];

}

直接取到lat和lng 的节点

NSString *googleURL
= [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=true",

[searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

CLLocationCoordinate2D position;

position.latitude = 0.0;

position.longitude = 0.0;

NSError *error;

NSString *retstr
= [NSString stringWithContentsOfURL:[NSURL URLWithString:googleURL]encoding:NSUTF8StringEncoding
error:&error];

if (retstr)

{

NSDictionary *dict = [retstr JSONValue];

if (dict)

{

NSArray *results = [dict objectForKey:@"results"];

if (results && results.count > 0)

{

NSDictionary *geometry = [[results objectAtIndex:0] objectForKey:@"geometry"];

NSDictionary *locate = [geometry objectForKey:@"location"];

position.latitude = [[locate objectForKey:@"lat"] floatValue];

position.longitude = [[locate objectForKey:@"lng"] floatValue];

NSLog(@"lat%f",position.latitude);

NSLog(@"lng%f",position.longitude);

}

}

[self gotoLocation:position.latitude and:position.longitude];

}

else

{

NSLog(@"error: %@", error);

}

NSLog(@"refasdfasdfasdfasdf:%@",retstr);

return ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: