首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤其他NSDictionary中的NSDictionary

过滤其他NSDictionary中的NSDictionary
EN

Stack Overflow用户
提问于 2013-03-12 10:49:57
回答 1查看 140关注 0票数 0

我有一本字典,里面有从解析JSON中提取的其他字典中的子词典。结构如下:

代码语言:javascript
复制
    {
        children =             (
                            {
                children =                     (
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = biological;
                        urlId = 9950000123891;
                    },
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = White;
                        urlId = 9950000123892;
                    },
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = "various flavors";
                        urlId = 9950000123893;
                    },
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = "different tastes creamy";
                        urlId = 9950000123894;
                    },
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = "yogurt drinks";
                        urlId = 9950000123895;
                    },
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = "Yogurt mix";
                        urlId = 9950000123896;
                    },
                                            {

                );
                hasArticles = 0;
                hasWideIcon = 0;
                label = "types of yogurt"; //those above are the children of the "types of yogurt" 
                urlId = 9950000123890;
            },


                            {
                children =                     (
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = White;
                        urlId = 9950000123906;
                    },
                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = "various flavors";
                        urlId = 9950000123907;
                    },

                                            {
                        hasArticles = 1;
                        hasWideIcon = 0;
                        label = Pappareale;
                        urlId = 9950000123909;
                    }
                );
                hasArticles = 0;
                hasWideIcon = 0;
                label = "yogurt healthy"; //those above are the children of the yogurt healthy 
                urlId = 9950000123905;
            },

                            {
                hasArticles = 1;
                hasWideIcon = 0;
                label = "puddings and creams";
                urlId = 9950000123911;
            },
                            {
                hasArticles = 1;
                hasWideIcon = 0;
                label = "Snack dessert";
                urlId = 9950000123913;
            },
                            {
                hasArticles = 1;
                hasWideIcon = 0;
                label = "various Dessert";
                urlId = 9950000123914;
            }
        );
        hasArticles = 0;
        hasWideIcon = 0;
        label = "Yogurt and Dessert ";
        urlId = 9950000123889;
    },

我的代码

代码语言:javascript
复制
 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{

        NSLog(@"%d", [webData length]);

       NSString *strResult =[[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
       NSDictionary *result =[strResult JSONValue];
           for (id obj in result){
                  NSLog(@"%@", result);
                  /*A part of the result is written above, but in reality is much 
                    longer and includes all possible products in a supermarket*/

           }

   }

如何提取"yogurtDessert“的子级

  • 酸奶种类(谁有孩子)
  • 酸奶-健康(谁有孩子)
  • 布丁和奶油(没有孩子)
  • 小吃甜点(没有孩子)
  • 各种甜点(无子女)

那么,我该如何提取yogurtDessert孩子们事先不知道的信息呢?

我需要创建一个包含超市产品类别的字典数组(在本例中,只有酸奶和甜点,共有23个元素),然后我必须创建另一组字典,每个字典包含每个产品的子类别,即数千个。我曾想过使用NSPredicate,但都是字典。我必须在其他字典中过滤字典。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-12 11:07:33

字典的层次结构似乎包含两种类型的字典:代表一个类别的字典和作为产品的字典。

我在这里猜叶子字典(没有孩子)总是产品。拥有子键的字典总是分类的。这可能不是真的--看起来hasArticles对于产品是1,对于类别是0--但是,这还是未知的。

以下是提取所有产品的代码:

代码语言:javascript
复制
static void collectProducts(NSDictionary *dictionary, NSMutableArray *results)
{
    NSArray *children == dictionary[@"children"];

    if (children == nil) {
        [results addObject:dictionary];
        return;
    }

    for (NSDictionary *subDict in children)
        collectProducts(subDict, results);
}

NSArray *findProducts(NSDictionary *dictionary)
{
    NSMutableArray *result = [NSMutableArray array];
    collectProducts(dictionary, result);
    return array;
}

用您的JSON findProducts result调用result以获得所有的叶字典。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15359162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档