如何解码JSON输出数据中的路由点​​?

时间:2012-04-11 05:06:54

标签: ios xcode json google-maps mkmapview

我是ios字段中的新用户,现在我正在尝试实施基于MKMapView的iPhone应用程序。在Google Map Web服务中,我以JSON输出格式获取数据。

- (void)update {

    self.responseData=[NSMutableData data];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=Ernakulam&destination=Kanyakumari&mode=driving&sensor=false"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];    
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [self.responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [connection release];
    self.responseData =nil;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"connection established");
    [connection release];
    NSString *responseString=[[NSString alloc]initWithData:self.responseData encoding:NSUTF8StringEncoding];
    self.responseData=nil;
    NSLog(@" OUTPUT JSON DATA^^^^^^^^^^^^^^^^%@",responseString);


}  

我得到了JSON格式的输出。现在我想画这两个位置之间的路线。播种如何从JSON输出数据中分离所有纬度和经度点。

2 个答案:

答案 0 :(得分:2)

查看此代码。我正在使用JSONkit进行解析

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"connection established");
    [connection release];
    NSString *responseString=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
    NSMutableDictionary *data1 = [responseData objectFromJSONData];
    NSMutableArray *ad = [data1 objectForKey:@"routes"];
    NSMutableArray *data2 = [[ad objectAtIndex:0] objectForKey:@"legs"];
    NSMutableArray *data3 = [[data2 objectAtIndex:0] objectForKey:@"steps"];
//    NSLog(@"Data3 %@", data3);
    for(int i = 0; i<data3.count;i++){
        NSLog(@"Start %@", [[data3 objectAtIndex:i] objectForKey:@"start_location"]);
        NSLog(@"End %@", [[data3 objectAtIndex:i] objectForKey:@"end_location"]);

    }

}  

答案 1 :(得分:0)

我正在使用SBJson来解析json数据。 通常,调用[responseString JSONValue]来获取已解析的数据。 您可以谷歌搜索详细信息。