如何在iPhone中不使用来自NSDictionary / NSMutableDictionary的Key获取值?

时间:2011-11-25 11:21:30

标签: iphone json web-services nsdictionary

我有一个Json(Key和Value方法)格式的webservice响应。我解析并获得最大的webservice方法。但是,在一个webservice方法中,我无法从key获取值。在这里我附上样本回复,

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

我已经获得了关键课程的价值,价值是,

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

现在,我需要获取密钥date,cost和brief_desc的值。值以上是NSDictionary。因此,我使用此代码来获取关键日期,成本和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);

在NSlog中,我得到了字典方法中的值。 sDateArray值和计数是,

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

我如何解析并获取密钥date,brief_desc和cost的确切值?我需要在UITableView中加载这些项目。请帮我解决这个问题。提前谢谢。

4 个答案:

答案 0 :(得分:3)

感谢所有观看我的问题并回答我的朋友们。我用这段代码解决了这个问题。我特别感谢Mr.Mattias Wadman,Mr.Adedoy先生和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]);
}

此代码解决了我的问题。感谢。

答案 1 :(得分:0)

我认为您在valueForKey上使用NSArray,您认为这是NSMutableDictionary。结果是valueForKey被发送到数组中的所有元素,返回一个带有值的新数组。

[lessonDic valueForKey:@"ObjectiveC Book"];是一个数组而不是字典。

顺便说一下,在使用它们之前,您不需要为lessonDicBookDic执行alloc / init。您可能应该阅读有关Objective-C和Cocoa内存管理的内容。

答案 2 :(得分:0)

我认为你应该使用由字典组成的数组:

//in you .h 
NSArray objectiveBook

然后你在你.m中初始化它并获得json。类似的东西:

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

这样你就可以在tableView中使用它的大小

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

在自定义表格视图单元格的外观时:

- (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; }

你去吧。我假设你已经在你的笔尖设置了uitableview。

答案 3 :(得分:0)

您可以使用 allKeys allValues 方法

如果您想按名称排序键,可以使用下一个示例

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

使用示例:

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