How to get Values without using Key from NSDictionary/NSMutableDictionary in iPhone?

爷,独闯天下 提交于 2019-12-02 02:20:24

问题


I have a webservice response in Json(Key and Value method) format. I parsed and get maximum of webservice methods. But, in one webservice method i can not get the values from key. Here i attaching the sample response,

lessons =     (
                {  
            "ObjectiveC Book" =             (
                                {
                    "brief_desc" = "ObjectiveC";
                    cost = 20;
                    date = "2011-09-06";
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    cost = 20;
                    date = "2011-09-07";
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    cost = 20;
                    date = "2011-09-09";
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    cost = 20;
                    date = "2011-09-10";
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    cost = 20;
                    date = "2011-09-14";
                }
            );
        }
    );

I have get the value for the key lessons, the value is ,

{  
                "ObjectiveC Book" =             (
                                    {
                        "brief_desc" = "ObjectiveC";
                        cost = 20;
                        date = "2011-09-06";
                    },
                                    {
                        "brief_desc" = "ObjectiveC";
                        cost = 20;
                        date = "2011-09-07";
                    },
                                    {
                        "brief_desc" = "ObjectiveC";
                        cost = 20;
                        date = "2011-09-09";
                    },
                                    {
                        "brief_desc" = "ObjectiveC";
                        cost = 20;
                        date = "2011-09-10";
                    },
                                    {
                        "brief_desc" = "ObjectiveC";
                        cost = 20;
                        date = "2011-09-14";
                    }
                );
            }

Now , i need to get the values for the keys date, cost and brief_desc. Above the values are in NSDictionary. So, i used this code to get the values for the key date,cost and bried_desc.

 NSDictionary *lessonDic = [[NSDictionary alloc] init];
    lessonDic = [latestLoans valueForKey:@"lessons"];
    NSLog(@"LessonsDic : %@", lessonDic);

    NSMutableDictionary *BookDic = [[NSMutableDictionary alloc] init];
    BookDic = [lessonDic valueForKey:@"ObjectiveC Book"];
    NSLog(@"StudentDic : %@, Count : %d", BookDic, [BookDic count]);

    NSArray *sDateArray = [BookDic valueForKey:@"date"];
    NSLog(@"sDateArray : %@", sDateArray);

In NSlog i got the values in dictionary method. the sDateArray values and count is,

(
        "2011-09-06",
        "2011-09-07",
        "2011-09-09",
        "2011-09-10",
        "2011-09-14"
    )
count : 1

How can i parse and get the exact values for the keys date, brief_desc and cost? I need to load these items in UITableView. Please help me to solve this problem. Thanks in advance.


回答1:


Thanks for all my friends who viewed my question and answered me. I solved this problem with this code. My special thanks to Mr.Mattias Wadman,Mr.Adedoy and Mr.inot.

NSArray *items = [lessonDicti valueForKeyPath:@"ObjectiveC Book"];
NSLog(@"Items : %@, Count  :%d", items, [items count]);
NSEnumerator *enumerator = [items objectEnumerator];
NSDictionary* item;
while (item = (NSDictionary*)[enumerator nextObject]) 
{
    NSLog(@"Item Dict : %@", item);
    sDateArray = [item valueForKey:@"date"];
    NSLog(@"sDateArray : %@", sDateArray);
    descArray = [item valueForKey:@"brief_desc"];
    cstArray = [item valueForKey:@"cost"];
    NSLog(@"eventStatusArray : %@, Count : %d", cstArray, [cstArray count]);
}

This code has solved my problem. Thanks.




回答2:


I think you should use an array composed of dictionary like:

//in you .h 
NSArray objectiveBook

Then you initialize it in you .m and get the json. Something like:

objectiveBook = [lessonDic valueForKey:@"ObjectiveC Book"];

So that you can use its size in the tableView

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [objectiveBook count];  }

And in customizing the appearance of table view cells.:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"cell";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//blah blah..

    NSDictionary *temp = [objectiveBook objectAtIndex:indexPath.row];

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 170, 60)];
    //initialize other labels here...

//gets the values you want
lbl1.text = [temp objectForKey:@"brief_desc"];
lbl2.text = [temp objectForKey:@"cost"];
lbl3.text = [temp objectForKey:@"date"];
//display it
[cell.contentView addSubview:lbl1];
[cell.contentView addSubview:lbl2];
[cell.contentView addSubview:lbl3];

return cell; }

There you go. I assume you already set the uitableview in your nib.




回答3:


I think you are using valueForKey on a NSArray which you think is a NSMutableDictionary. The result will be that valueForKey is sent to all elements in the array a new array with the values is returned.

[lessonDic valueForKey:@"ObjectiveC Book"]; is an array not a dictionary.

BTW, you don't need to do alloc/init for lessonDic and BookDic before using them. You should probably read up a bit about both Objective-C and Cocoa memory management.




回答4:


you can use allKeys and allValues methods

if you want sort keys by name you can use next example

static NSInteger alphabeticSortByLabelInDictionary_pad(NSDictionary *dict1, NSDictionary *dict2, void *reverse) {
    NSString *label1 = [dict1 objectForKey:@"Label"];
    NSString *label2 = [dict2 objectForKey:@"Label"];

    if (*(BOOL *)reverse == YES) {  
        return [label2 localizedCaseInsensitiveCompare:label1];
    }
    return [label1 localizedCaseInsensitiveCompare:label2];
}

filteredListContent - NSArray

Use example:

BOOL reverseSort = NO;
    list = [filteredListContent sortedArrayUsingFunction:alphabeticSortByLabelInDictionary_pad context:&reverseSort];


来源:https://stackoverflow.com/questions/8268366/how-to-get-values-without-using-key-from-nsdictionary-nsmutabledictionary-in-iph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!