如何从 iPhone NSDictionary 中的未知键获取值?

发布于 2024-12-17 21:39:59 字数 1455 浏览 0 评论 0原文

在解析 Web 服务响应的值时,我还有一个大问题。我不知道网络服务响应中值的键。例如,

class =     (
                {
            "ObjectiveC" =   
         (
                                {
                    "brief_desc" = "ObjectiveC";
                    date = "2008-02-27";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    date = "2008-03-05";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    date = "2008-03-12";
                    "event_status" = Missed;
                },
            ); 
        },
                {
            "Java" =             (
 {
                    "brief_desc" = "Java";
                    date = "2008-02-27";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "Java";
                    date = "2008-03-05";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "Java";
                    date = "2008-03-12";
                    "event_status" = Missed;
                },
            );
        }
    );

在这个响应中,我们甚至不知道键“ObjectiveC”和“Java”。键(“ObjectiveC 和 Java”)应该在每个返回的响应中更改。如何获取键(未知键)的值?我如何解析这个响应并获取值?

I have one more big problem while parse the values from Webservice response. I dont know the key for the values in the webservice response. For example,

class =     (
                {
            "ObjectiveC" =   
         (
                                {
                    "brief_desc" = "ObjectiveC";
                    date = "2008-02-27";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    date = "2008-03-05";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "ObjectiveC";
                    date = "2008-03-12";
                    "event_status" = Missed;
                },
            ); 
        },
                {
            "Java" =             (
 {
                    "brief_desc" = "Java";
                    date = "2008-02-27";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "Java";
                    date = "2008-03-05";
                    "event_status" = Attended;
                },
                                {
                    "brief_desc" = "Java";
                    date = "2008-03-12";
                    "event_status" = Missed;
                },
            );
        }
    );

In this response even we dont know the keys "ObjectiveC" and "Java". The keys("ObjectiveC and Java") should be change in every response retured. How to get values of key (Unknown key)? How can i parse this response and get the values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-12-24 21:39:59

我会通过键枚举来获取值。这是 Objective-c 中枚举的示例。

NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
    //assuming value will be string
    NSString *valueForKey = [myDictionary valueForKey:key]; 

    //assuming value is another dictionary
    NSDictionary *subDictionary = [myDictionary objectForKey:key];
}

如果您不知道子词典的键,您也可以通过它们进行枚举。

I would enumerate through the keys to get the value. Here is an example of enumeration in objective-c.

NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
    //assuming value will be string
    NSString *valueForKey = [myDictionary valueForKey:key]; 

    //assuming value is another dictionary
    NSDictionary *subDictionary = [myDictionary objectForKey:key];
}

And if you don't know the keys of the sub dictionaries, you can enumerate through those as well.

陌生 2024-12-24 21:39:59

NSDictionary 有 - (NSArray *)allKeys- (NSArray *)allValues。其中之一可能会有所帮助

NSDictionary * lessonDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Key", @"Value", @"Key 2", @"Value 2", nil];
    NSArray *values = [lessonDict allValues];

    NSArray *keys = [lessonDict allKeys];
    NSLog(@"Keys: %@",keys);
    NSLog(@"Values: %@",values);

NSDictionary has - (NSArray *)allKeys and - (NSArray *)allValues. One of those might help

NSDictionary * lessonDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Key", @"Value", @"Key 2", @"Value 2", nil];
    NSArray *values = [lessonDict allValues];

    NSArray *keys = [lessonDict allKeys];
    NSLog(@"Keys: %@",keys);
    NSLog(@"Values: %@",values);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文