简体   繁体   English

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

[英]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". 我在一个项目中工作,在该项目中,我有一个包含以给定格式的字典的数组,我必须在“ size_key”的基础上获取“ Size_val”的值。

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". 我只想基于“ size_key”获取“ Size_val”的值。 i use the code to get the value of uppercatname. 我使用代码来获取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. 显然,您的size值也是一个数组,因此您必须先获取此数组,然后循环通过sizes数组以获取size字典,然后从size字典中获取具有您的size_key的正确size_val。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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