解析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 ;
//一层一层的解析到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 ;
相关文章推荐
- 在子窗口中刷新父窗口 JAVASCRIPT
- jsp页面分页
- JS Replace 全部替换字符 用法
- JavaScript学习笔记(四)DOM
- Extjs 提示
- 2014年2月12个最佳javascript类库
- 关于JavaScript中Get/Set访问器
- javascript的window.onload方法使用
- JS获取一个字符串的字节长度
- js获取屏幕的宽度高度以及window.open的用法
- Html.CSS.JavaScript 学习经验
- 关于JS点击button之灵活替换改变内容方法
- js的封装,闭包【--之封装】[[6]]
- 【js学习笔记-109】-------SVG 可伸缩的适量图形
- JS 获取时间
- 使用Jsoup解析html网页
- ExtJs中Store加载(load)时候提示信息(自定义waitMsg--加载完毕关闭提示框)
- javascript 复制粘贴操作
- 在jsp中使用xheditor
- JS 保留小数点后面2位小数