Abhinandan Pratap
Abhinandan Pratap

Reputation: 2148

IOS: how to get Dictionary value which is inside an array

i am working on a project in which i have an array which contain a dictionary which is in the given form i have to get the value of "Size_val" on the basis of "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;
    };

i just only want to get the value of "Size_val" on the basis of "size_key". i use the code to get the value of uppercatname.

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

please give me some solution how to get this value

Upvotes: 0

Views: 504

Answers (4)

TheEye
TheEye

Reputation: 9346

Apparently your sizes values is also an array, so you have to get this array first, then loop through the sizes array to get the size dictionaries, and from the size dictionaries get the right size_val for the size_key you have.

So for the first entry and your size key:

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
            ...
    }
}

Upvotes: 1

dk_checkitPortal
dk_checkitPortal

Reputation: 126

Please try my below coding

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

Upvotes: 1

Islam
Islam

Reputation: 3734

You can get it like this:

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
    }
}

Upvotes: 1

user3182143
user3182143

Reputation: 9589

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"]];
}

Upvotes: 1

Related Questions