解析json串

假如要解析出json串中的lat和lng。可有以下两种方法,一种是层层解析,一种是直接取其根节点。解析json串

//一层一层的解析到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:0objectForKey:@"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 ;    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值