0

解析JSONから抽出された他の辞書の子を持つ辞書があります。構造は次のようになります。

    {
        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;
    },

私のコード

 -(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の使用を考えましたが、辞書です。他の辞書で辞書をフィルタリングする必要があります

4

1 に答える 1

2

辞書の階層には、カテゴリを表す辞書と製品である辞書の2種類の辞書が含まれているようです。

ここでは、葉の辞書(子供なし)は常に製品であると推測しています。子キーを持つ辞書は常にカテゴリです。それは真実ではないかもしれません—hasArticles製品の場合は1、カテゴリの場合は0のように見えます—しかし、繰り返しになりますが、それは不明です。

すべての製品を抽出するコードは次のとおりです。

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

findProductsJSONを使用して呼び出し、resultすべての葉の辞書を取得します。

于 2013-03-12T11:07:33.740 に答える