1

tableviewcellsがあり、ラベルとしてさまざまな「キー値」がロードされているビューがあります。1つをタップすると、別のビューが開きます。ただし、ここでは、そのキーだけの辞書を渡します。たとえば、次のように渡します。

{
    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";
        };
    };
}

これらの各値を取得するにはどうすればよいですか?ありがとう。

注意:値の配列のタイトルは変更される可能性があります...さらに追加したり、削除したりできるので、一般的な解決策が必要です。

4

3 に答える 3

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);
    }
}
于 2012-11-14T06:39:40.283 に答える
1
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

NSArray *arrBudget= [jsonDictionary objectForKey:@"Budget"];

したがって、ここarrBudgetにはすべての値が含まれ、配列を詳細ビューに渡すことができます。

于 2012-11-14T06:27:27.207 に答える
0

キーとオブジェクトが「foreach」ロジックで役立つ場合の別のアプローチ:

NSDictionary *dict = @{
    @"key1": @"value1",
    @"key2": @"value2",
    @"key3": @"value3",
};  
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Your key : %@", key);
    NSLog(@"Your value : %@", [obj description]);
    // do something...
}];
于 2014-10-24T12:43:20.353 に答える