gpt4 book ai didi

ios - 过滤其他 NSDictionary 中的 NSDictionary

转载 作者:行者123 更新时间:2023-11-28 22:38:08 29 4
gpt4 key购买 nike

我有一本字典,其中包含从解析 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”的 child

  • 酸奶的种类(有 child 的)
  • yogurt-healty(有 child )
  • 布丁和奶油(没有 child )
  • 点心甜品(不含 child )
  • 各种甜点(不含 child )

我如何提取事先不知道的 yogurtDessert 的 child ?

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

最佳答案

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

我在这里猜测叶子字典(没有 child )总是产品。具有子键的字典始终是类别。这可能不是真的——看起来 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;
}

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

关于ios - 过滤其他 NSDictionary 中的 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359162/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com