在foreach中获取NSDictionary的值?

178*_*040 1 json nsdictionary ios

我有一个视图,其上有tableviewcells,加载了不同的"键值"作为标签.当我点击一个时,我打开另一个视图.但是在这里,我只传递了那个键的字典,例如我会传递这个:

{
    key = Budget;
    value =     {
        "2012 Budget Report" =         {
            active = 0;
            author = "xxxxx xxxxxx";
            date = "October 27, 2012";
            description = "Example";
            dl = "53 downloads";
            email = "xxx@xxxxx.com";
            ext = DOCX;
            fortest = "Tuesday, November 6";
            id = 5;
            subject = budget;
            testdate = "Tuesday, November 6";
            title = "Budget spreadSheet";
        };
        "2005 - 2008 Budget Report" =         {
            active = 0;
            author = "xxxxxxx xxxxx";
            date = "November 3, 2012";
            description = "Example";
            dl = "18 downloads";
            email = "xxxxx@xxxxx.com";
            ext = DOCX;
            title = "Budget report";
        };
    };
}
Run Code Online (Sandbox Code Playgroud)

我如何获得这些值?谢谢.

请注意:值数组中的标题可能会更改...可以添加更多,可以删除一个,所以我需要一个通用的解决方案.

Ros*_*hit 11

考虑您传递的字典保存在iDictionary.

NSDictionary *iDictionary // Input Dictionary;
NSDictionary *theValues = [NSDictionary dictionaryWithDictionary:[iDictionary valueForKey:@"value"]];

for (NSString *aKey in [theValues allKeys]) {
    NSDictionary *aValue = [theValues valueForKey:aKey];
    NSLog(@"Key : %@", aKey);
    NSLog(@"Value : %@", aValue);

    // Extract individual values
    NSLog(@"Author : %@", [aValue objectForKey:@"author"]);

    // If the titles are dynamic
    for (NSString *aSubKey in [aValue allKeys]) {
        NSString *aSubValue = [aValue objectForKey:aSubKey];

        NSLog(@"SubKey : %@, SubValue = %@", aSubKey, aSubValue);
    }
}
Run Code Online (Sandbox Code Playgroud)