在NSDictionary中嵌套级别

时间:2014-03-03 05:13:44

标签: ios iphone objective-c xml-parsing

我从网络获取数据,然后创建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个对象,它们有自己的Number,Places,Seats,但是在ClassService下它们只有两个。我怎么知道Number 10的对象有

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

Tariff =             {
                    text = 1122;

Number 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`

。我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

Number 10和Number 11对象属于同一ClassService和Tariff,这就是它包含在数组中的原因,如此()。

你可以使用

[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"]]

    }
}