简体   繁体   中英

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

请尝试我下面的编码

NSDictionary *dict = (NSDictionary *)[[[arrFilteredOrder objectAtIndex:0] objectForKey:@"as_per_size"] objectForKey:@"sizes"];
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"]];
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM