簡體   English   中英

如何從NSData獲取詳細信息

[英]How to fetch details from NSData

我想從下面的URL獲取指定的數據html_instructions 我可以從該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];
    });

將此用作:

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

                                 options:kNilOptions
                                 error:&error];

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

希望對您有幫助。

我從以下網址解析了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(@"############################");
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM