如何获取NSArray键值作为NSString



我有很多类似的NSArray但我不确定如何获取每个数组的键值例如BootsDress Shoes

编辑:我不试图得到Boots的值,我试图得到的关键Boots, Dress Shoes等,而不是里面的东西。

 test {
    Boots =     (
        Brogue,
        Chelsea,
        Chukka,
        Dress,
        "Rain/Snow",
        Rugged
    );
    "Dress Shoes" =     (
        Oxfords,
        "Formal Shoes"
    );
    "Loafers/Slip-ons" =     (
        "Slip-ons",
        Loafers
    );
    Sandals =     (
        "Flip Flops",
        Slides
    );
    Sneakers =     (
        "Low Top",
        "High Tops",
        "Running Shoes",
        "Slip-ons"
    );
}

我如何得到值的Boots, Dress Shoes,等?

由于您没有提供正确的JSON响应。我使用上面的JSON在我的项目中创建一个本地JSON,并使用它解析

请找到我在我的项目中使用的示例JSON文件

Sample.json

{
    "test": {
        "Boots": [
                  "Brogue",
                  "Chelsea",
                  "Chukka",
                  "Dress",
                  "Rain/Snow",
                  "Rugged"
                  ],
        "Dress Shoes": [
                        "Oxfords",
                        "Formal Shoes"
                        ],
        "Loafers/Slip-ons": [
                             "Slip-ons",
                             "Loafers"
                             ],
        "Sandals": [
                    "Flip Flops",
                    "Slides"
                    ],
        "Sneakers": [
                     "Low Top",
                     "High Tops",
                     "Running Shoes",
                     "Slip-ons"
                     ]
    }
}

我使用下面的代码来获取数组值。有两种方法可以解析和获取数组值。

方法1

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"json"];
    NSError * error;
    NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }
    NSDictionary *dataList = [NSJSONSerialization JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:NULL];
    NSLog(@"%@",dataList);
    NSDictionary *sourceData = [dataList objectForKey:@"test"];
    NSArray * boots = [sourceData objectForKey:@"Boots"];
    NSArray * shoes = [sourceData objectForKey:@"Dress Shoes"];
    NSArray * loafers = [sourceData objectForKey:@"Loafers/Slip-ons"];
    NSArray * sandals = [sourceData objectForKey:@"Sandals"];
    NSArray * sneakers = [sourceData objectForKey:@"Sneakers"];
    NSLog(@"n %@ n %@ n %@ n %@ n %@",boots,shoes,loafers,sandals,sneakers);

方法2

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"json"];
        NSError * error;
        NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
        if(error)
        {
            NSLog(@"Error reading file: %@",error.localizedDescription);
        }
        NSDictionary *dataList = [NSJSONSerialization JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:NULL];
        NSLog(@"%@",dataList);
        NSDictionary *sourceData = [dataList objectForKey:@"test"];
NSArray *keyValues = [sourceData allKeys];
    for (int i =0 ; i < keyValues.count; i++) {
        NSArray *innerContents = [sourceData objectForKey:[keyValues objectAtIndex:i]];
        NSLog(@"%@ n", innerContents);
    }

最新更新