如何将NSArray键值作为NSString?

时间:2016-11-04 00:08:01

标签: objective-c nsarray

我有很多NSArray,但我不知道如何获取每个数组的键值,如BootsDress Shoes

编辑:我没有尝试获取Boots我试图获取密钥BootsDress 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"
    );
}

如何获得价值BootsDress Shoes等?

1 个答案:

答案 0 :(得分:0)

由于您未能提供正确的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);
    }