复杂的 NSDictionary。获取键的对象并将它们排序为 NSArray 或单独的变量?

发布于 2024-12-25 07:27:30 字数 2551 浏览 0 评论 0原文

我有一个从 XML 转换而来的 NSDictionary,这让我很困惑。如果您能看一下并让我知道获取键 class_id 的对象的最佳方法,我将非常感激。

 {
     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 崩溃,要么不返回任何数据。非常感谢您的帮助。

好吧,让我们备份一下。看来我用来将其转换为 NSDictionary 的 XMLReader 插件使这比实际需要的更加复杂。这是原始的 XML 数据。

<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。

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

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.

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>

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.

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

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

发布评论

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

评论(1

宫墨修音 2025-01-01 07:27:30

不要担心“最好的方法”。只需使用一种不会在以后重新访问时造成混乱的方法,并让您继续处理更重要的事情。

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

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"]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文