本文介绍了复杂的 NSDictionary.将objectsforkeys 排序为NSArray 或单独的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 XML 转换而来的 NSDictionary,这让我很困惑.如果您能看一下并告诉我获取关键 class_id 对象的最佳方法,我将不胜感激.

I have an NSDictionary that was converted from XML that is really confusing me. If you could take a look and let me know the best method of getting the objects for the key class_id, I would really appreciate it.

 {
     response=  {
        "@status" = ok;
        rosters =         {
                     "@page" = 1;
                     "@page_count" = 1;
                     "@records_per_page" = 50;
                     "@total_record_count" = 8;
                     roster =               (
                                            {
                                       "@class_id" = 0001;
                                       "@role" = 0;
                                       "@user_id" = 12345;
                                       class=                  {
                                                                "@active" = false;
                                                                "@name" = NAME;
                                                               }
                                            }
                                            {
                                       "@class_id" = 0002;
                                       "@role" = 0;
                                       "@user_id" = 12345;
                                            }
                                            {
                                       "@class_id" = 0003;
                                       "@role" = 0;
                                       "@user_id" = 12345;
                                            }
                                            );
                             }
                 }
}

我想要做的就是为各个 class_id 设置变量或将所有 class_id 放在 NSArray 中.我一直在阅读苹果文档并尝试了我能想到的所有方法,但它要么因 nsexception 而崩溃,要么不返回任何数据.非常感谢您的帮助.

All I am wanting to do is either set up variables for the individual class_id's or put all of the class_id's in an NSArray. I've been reading the apple documentation and have tried every method I can think of, but it either crashes with nsexception or returns no data. Thanks so much for your help.

好的,让我们备份一分钟.看起来我用来将它转换为 NSDictionary 的 XMLReader 插件使这比它需要的复杂得多.这是原始 XML 数据.

Ok let's just backup a minute. It would appear that the XMLReader plugin that I used to convert this to an NSDictionary is making this far more complicated than it needs to be. Here is the original XML data.

<response status="ok">
<rosters page_count="1" page="1" records_per_page="50" total_record_count="7">
<roster class_id="0001" role="S" user_id="91563" roster_id="ID"/>
<roster class_id="0002" role="S" user_id="91563" roster_id="ID"/>
<roster class_id="0003" role="S" user_id="91563" roster_id="ID"/>
<roster class_id="0004" role="S" user_id="91563" roster_id="ID"/>
<roster class_id="0005" role="S" user_id="91563" roster_id="ID"/>
<roster class_id="0006" role="S" user_id="91563" roster_id="ID"/>
<roster class_id="0007" role="S" user_id="91563" roster_id="ID"/>
</rosters>
</response>

谁能提出一种简单的方法来获取 class_id 值?转换为 NSDictionary 是一种合理的方法吗?我只需要数据作为单独的变量或 NSArray.

Can anyone suggest a simple way to just grab the class_id values? Is converting to NSDictionary a reasonable way to do it? I just need the data as separate variables or an NSArray.

推荐答案

不要担心最佳方法".只需使用一种不会混淆的方法,以便稍后重新访问,并让您继续处理更重要的事情.

Don't worry about the "best method". Just use a method that won't be confusing to revisit later and lets you move on to more important things.

NSMutableArray *classIds = [NSMutableArray array];
NSDictionary *rosterArray = [convertedXmlDictionary valueForKeyPath:@"response.rosters.roster"];
for (NSDictionary *roster in rosterArray) {
    [classIds addObject:[roster objectForKey:@"@class_id"]];
}

这篇关于复杂的 NSDictionary.将objectsforkeys 排序为NSArray 或单独的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:45