IOS:如何获取数组内的Dictionary值

时间:2015-12-07 07:39:06

标签: ios nsarray nsdictionary

我正在开发一个项目,其中我有一个包含字典的数组 在给定的形式中,我必须得到" Size_val"的值。在" size_key"。

的基础上
array : (
        {
        "as_per_size" =         {
            lowercatname = abcd;
            sizes =             (
                                {
                    "size_key" = "UP_CHEST";
                    "size_name" = "UP_CHEST";
                    "size_val" = 4;
                    typecat = 1;
                },
                            {
                "size_key" = LENGTH;
                "size_name" = LENGTH;
                "size_val" = 1;
                typecat = 1;
            },
                            {
                "size_key" = DP;
                "size_name" = DP;
                "size_val" = 3;
                typecat = 1;
            },
                            {
                "size_key" = "YOKE_LENGTH";
                "size_name" = "YOKE_LENGTH";
                "size_val" = 2;
                typecat = 1;
            },
                            {
                "size_key" = KNEE;
                "size_name" = KNEE;
                "size_val" = 2;
                typecat = 2;
            },
                            {
                "size_key" = THIGH;
                "size_name" = THIGH;
                "size_val" = 1;
                typecat = 2;
            },
                            {
                "size_key" = CALF;
                "size_name" = CALF;
                "size_val" = 3;
                typecat = 2;
            }
        );
        uppercatname = CHOLI;
    };

我只想获得" Size_val"的价值。在" size_key"的基础上。 我使用代码获取uppercatname的值。

upstring1 = [[[arrFilteredOrder objectAtIndex:0] objectForKey:@"as_per_size"] objectForKey:@"uppercatname"];
            TypeLabel.text = upstring1;

请给我一些解决方法如何获得此值

4 个答案:

答案 0 :(得分:1)

请尝试以下编码

NSDictionary *dict = (NSDictionary *)[[[arrFilteredOrder objectAtIndex:0] objectForKey:@"as_per_size"] objectForKey:@"sizes"];

答案 1 :(得分:1)

NSArray *arr = [[[[array objectAtIndex:0]objectForKey:@"as_per_size"]objectForKey:@"sizes"]copy];

for(int i=0;i<[arr count];i++)
{
   NSString *strSizekey = [NSString stringWithFormat:@"%@",[[arr objectAtIndex:i]objectForKey:@"size_key"]];
   NSString *strSizeValue = [NSString stringWithFormat:@"%@",[[arr objectAtIndex:i]objectForKey:@"size_val"]];
}

答案 2 :(得分:1)

显然你的size值也是一个数组,所以你必须首先得到这个数组,然后循环遍历sizes数组以获得大小字典,并从大小字典中获得你所拥有的size_key的正确size_val。 p>

因此,对于第一个条目和您的尺寸密钥

NSArray* sizes = [[[arrFilteredOrder[0] objectForKey:@"as_per_size"] objectForKey;@"sizes];

foreach(NSDictionary* sizeDict in sizes {
    NSString* sizeKey = sizeDict[@"size_key];
    if([sizeKey isEqualToString:<your size key>])
    {
            NSString* sizeVal = sizeDict[@"size_val"];
            // now do something with your value
            ...
    }
}

答案 3 :(得分:1)

你可以这样:

NSString *sizeValue = nil;

NSArray *sizes = [arrFilteredOrder.firstObject valueForKeyPath:@"as_per_size.sizes"];
for (NSDictionary *size in sizes) {
    if ([size[@"size_key"] isEqualToString:sizeKey]) {
        sizeValue = [size[@"size_val"] description]; //converting to string
    }
}