如何在iPhone中不使用来自NSDictionary / NSMutableDictionary的Key获取值? [英] How to get Values without using Key from NSDictionary/NSMutableDictionary in iPhone?

查看:64
本文介绍了如何在iPhone中不使用来自NSDictionary / NSMutableDictionary的Key获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在,我需要获取密钥的日期,成本和brief_desc的值。值以上是NSDictionary。因此,我使用此代码获取关键日期,成本和bried_desc的值。

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

在NSlog中我得到了字典方法中的值。 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

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

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.

推荐答案

感谢所有观看我的问题并回答我的朋友。我用这段代码解决了这个问题。我要特别感谢Mr.Mattias Wadman,Mr.Adedoy先生和Mr.inot先生。

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.

这篇关于如何在iPhone中不使用来自NSDictionary / NSMutableDictionary的Key获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆