2016-11-04 7 views
-1

私はそうしかしBootsまたはDress ShoesNSArrayのキー値をNSStringとして取得する方法は?

EDITのように各配列のキーの値を取得する方法がないことを確認イムのような多くのNSArrayの年代を持っている:イムは、Dress ShoesBootsイムの値は、キーBootsを取得しようとして取得しようとしていませんそれらの中に何がないかなど。

test { 
    Boots =  (
     Brogue, 
     Chelsea, 
     Chukka, 
     Dress, 
     "Rain/Snow", 
     Rugged 
    ); 
    "Dress Shoes" =  (
     Oxfords, 
     "Formal Shoes" 
    ); 
    "Loafers/Slip-ons" =  (
     "Slip-ons", 
     Loafers 
    ); 
    Sandals =  (
     "Flip Flops", 
     Slides 
    ); 
    Sneakers =  (
     "Low Top", 
     "High Tops", 
     "Running Shoes", 
     "Slip-ons" 
    ); 
} 

どのように私は価値のBootsDress Shoesなどを得るのですか?

+0

名前でそれらにアクセスできるようにしたい場合は、NSDictionaryの中でそれを格納する方が良いと思います。そうでなければ、 'NSArray.objectAtIndex'を使用したいでしょう。 – brandonscript

+0

UITableViewの' objectAtIndex:indexPath.row'を試しましたが、クラッシュしました。 –

+0

@brandonscript誤解を招くかもしれませんが、ブーツ "キーを取得しようとしています –

答えて

0

正しいJSON応答を提供していないためです。私は私のプロジェクトでローカルのものを作成するためにあなたの上でJSONを使用し、それは私が

{ 
    "test": { 
     "Boots": [ 
        "Brogue", 
        "Chelsea", 
        "Chukka", 
        "Dress", 
        "Rain/Snow", 
        "Rugged" 
        ], 
     "Dress Shoes": [ 
         "Oxfords", 
         "Formal Shoes" 
         ], 
     "Loafers/Slip-ons": [ 
          "Slip-ons", 
          "Loafers" 
          ], 
     "Sandals": [ 
        "Flip Flops", 
        "Slides" 
        ], 
     "Sneakers": [ 
        "Low Top", 
        "High Tops", 
        "Running Shoes", 
        "Slip-ons" 
        ] 
    } 
} 
Sample.json私のプロジェクト

で使用するサンプルJSONファイルを見つけてください

を解析に使用

私が使用しました配列値を取得するコードの下にあります。配列値を解析して取得するには2つの方法があります。

アプローチ1

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"json"]; 

    NSError * error; 
    NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; 

    if(error) 
    { 
     NSLog(@"Error reading file: %@",error.localizedDescription); 
    } 

    NSDictionary *dataList = [NSJSONSerialization JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:NULL]; 

    NSLog(@"%@",dataList); 
    NSDictionary *sourceData = [dataList objectForKey:@"test"]; 
    NSArray * boots = [sourceData objectForKey:@"Boots"]; 
    NSArray * shoes = [sourceData objectForKey:@"Dress Shoes"]; 
    NSArray * loafers = [sourceData objectForKey:@"Loafers/Slip-ons"]; 
    NSArray * sandals = [sourceData objectForKey:@"Sandals"]; 
    NSArray * sneakers = [sourceData objectForKey:@"Sneakers"]; 

    NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@",boots,shoes,loafers,sandals,sneakers); 

アプローチ2

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"json"]; 

     NSError * error; 
     NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; 

     if(error) 
     { 
      NSLog(@"Error reading file: %@",error.localizedDescription); 
     } 

     NSDictionary *dataList = [NSJSONSerialization JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:NULL]; 

     NSLog(@"%@",dataList); 
     NSDictionary *sourceData = [dataList objectForKey:@"test"]; 

NSArray *keyValues = [sourceData allKeys]; 

    for (int i =0 ; i < keyValues.count; i++) { 
     NSArray *innerContents = [sourceData objectForKey:[keyValues objectAtIndex:i]]; 
     NSLog(@"%@ \n", innerContents); 
    } 
関連する問題