2014-03-03 116 views
0

我從網絡獲取數據,然後創建NSDictionary。這是我的NSDictionary的一部分:在NSDictionary中嵌套級別

Car =    (
           { 
        Number = 10; 
        Places =      { 
         text = "001,003,004,022,023,024,026,027,029,030,031,033,035,036,038,040,042,043,044,047,048,049,050,051,052"; 
        }; 
        Seats =      { 
         SeatsDn = 8; 
         SeatsLateralDn = 4; 
         SeatsLateralUp = 7; 
         SeatsUp = 6; 
        }; 
       }, 
           { 
        Number = 11; 
        Places =      { 
         text = "006,008,014,016,018,020,022,026,028,033,034,035,036,038,040,042,043,044,045,046,049,050,052"; 
        }; 
        Seats =      { 
         SeatsDn = 2; 
         SeatsLateralDn = 3; 
         SeatsLateralUp = 7; 
         SeatsUp = 11; 
        }; 
       } 
      ); 
      ClassService =    { 
       Type = "1_type"; 
       text = "First type descr"; 
      }; 
      Owner =    { 
       Country =     { 
        Code = 27; 
        Name = "Country name"; 
       }; 
       Type = "\U041a\U0417\U0425/\U041a\U0417\U0425"; 
      }; 
      Tariff =    { 
       text = 1122; 
      }; 
      TariffService =    { 
       text = 250; 
      }; 
      TrainLetter = "\U0425"; 
      Type = "\U041f\U043b\U0430\U0446\U043a\U0430\U0440\U0442\U043d\U044b\U0439"; 
     } 
    ); 

的問題是,有2名對象,他們有自己的編號,位置,座椅,但所有的東西ClassService下,他們有一個兩個。我怎麼會知道與10號該對象是否具有

ClassService = 
        Type = "1_type"; 
        text = "First type descr" 

,並

Tariff =    { 
        text = 1122; 

並與11號對象已

ClassService = 
        Type = "1_type"; 
        text = "First type descr" 

,並

Tariff =    { 
        text = 1122; 

現在我'這個:

NSarray *CarsNumbers = [[myDict valueForKey: @"Car"] valueForKey: @"Number"];` 
NSarray *CarsTariffs = [[[myDict valueForKey: @"Car"] valueForKey: @"Tariff"] valueForKey: @"text"]; 
This way I see that `CarsNumbers.count = 2, `CarsTariffs.count = 1` 

。我該怎麼做對不對?

回答

0

10號和11號對象在同一個ClassService和關稅中,這就是爲什麼它包含在數組中,就像this()。

你可以使用

[myDict valueForKey:@"ClassService"]valueForKey:@"Type"]]// Type = "1_type"; 

[myDict valueForKey:@"Tariff"]valueForKey:@"text"]]// text = 1122; 

NSArray *Cars = [myDict valueForKey:@"Car"]; 

for (NSDictionary *aCar in Cars) { 

    if ([aCar valueForKey:@"Number"] == 10) { 

     //your Car NSObject.ClassService = [myDict valueForKey:@"ClassService"]valueForKey:@"Type"]] 

    } 
} 
+0

10 - 只是例子,我不知道它會是什麼號碼 – daleijn