如何从JSON输出数据进行解码路线点?(How to decode route points fro

2019-09-16 11:33发布

我在我的新ios场,现在我想实现MKMapView基于iPhone的应用。 从谷歌地图的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了把数据。

Answer 1:

看看这个代码。 我使用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"]);

    }

}  


Answer 2:

我使用SBJson解析JSON数据。 在一般情况下,调用[responseString JSONValue]得到解析的数据。 你可以google一下详细的参考。



文章来源: How to decode route points from the JSON Output Data?