0

我想html_instructions从下面的 URL 中获取指定的数据。我可以从这个URL获取所有数据。

但我html_instructions只需要具体的。如何拆分数据?

我使用以下代码将 URL 转换为 NSData:

 NSString *url = [NSString      stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false"];
    NSURL *googleRequestURL=[NSURL URLWithString:url];
    dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
    });
4

2 回答 2

2

将此用作:

    NSData* data = [NSData dataWithContentsOfURL:googleRequestURL];
    if (data == nil) {
        return;
    }
    NSError* error;
    NSMutableDictionary* json = [NSJSONSerialization
                                 JSONObjectWithData:data

                                 options:kNilOptions
                                 error:&error];

    NSLog(@"Json : %@",json);

希望它可以帮助你。

于 2013-05-07T09:13:05.110 回答
1

我从以下 url 解析了 json 并打印了 html 说明尝试复制此代码并尝试: http ://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false

   NSData *receivedData = Received data from url;
   NSError* error;
   NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:receiveData              options:kNilOptions  error:&error];

           NSArray *allkeys = [parsedJson allKeys];

        for(int i = 0; i < allkeys.count; i++){
            NSLog(@"############################");
            if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){
                NSArray *arr        = [responseJsonValue objectForKey:@"routes"];
                NSDictionary *dic   = [arr objectAtIndex:0];
                NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
                NSArray *legs = [dic objectForKey:@"legs"];
                  NSLog(@"legs array count %d", legs.count);
                for(int i = 0;  i < legs.count; i++){
                    NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
                    for (int i = 0; i < stepsArr.count; i++) {
                        NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
                    }
                }

            }
            NSLog(@"############################");
        }
于 2013-05-07T09:23:15.860 回答