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

标签 iphone json web-services nsdictionary

我有一个 Json(键和值方法)格式的 Web 服务响应。我解析并获取了最多的 Web 服务方法。但是,在一种网络服务方法中,我无法从键中获取值。我在这里附上示例响应,

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

我已经得到了关键类(class)的值(value),值(value)是,

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

现在,我需要获取键日期、成本和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 中加载这些项目。请帮我解决这个问题。提前致谢。

最佳答案

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

这段代码解决了我的问题。谢谢。

关于iphone - 如何在 iPhone 中不使用 NSDictionary/NSMutableDictionary 中的 Key 来获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8268366/

相关文章:

ios - 延迟位置更新问题 ios

java - 如何在java中将JSON对象从RESTful客户端传递到服务器

c# - 从 C# 项目中的文件夹获取许可证文件

iphone - Objective C 属性的歧义

iphone - Apple 的开发者证书和配置文件

iphone - 如何使用 Phonegap 获取 iPhone 的唯一标识符 (UDID)?

java - 如何使用 jQuery 将 JSON 数据发布到 Struts2 Action 类

javascript - JSON 序列化一个 DOM 元素

apache - LiteSpeed VS OpenLiteSpeed

java - Google App Engine 是否允许第三方网络服务访问?