0

我有以下格式的两个 NSMutableDictionary 对象:

{products = {  sitedetails =   { currency = USD; };}

和这个 :

 {products =    { sitedetails =   { latestoffers =   { price =  { test = { gte = 100; };  }; };  }; };  }

我需要附加到一个看起来像这样的格式:

{products =    { sitedetails =   { { currency = USD; },latestoffers =   { price =  { test = { gte = 100; };  }; };  }; };  }

我必须以这样的方式附加它们,以便将两个 nsmutabedictionaries 的 sitedetails 对象添加为单个字典。

可能的对象和键

 [sem queryBuilderWithObj:@"4992" andKeyValue:@"cat_id" ];
 [sem queryBuilderWithObj:@"Toshiba" andKeyValue:@"brand"];
 [sem queryBuilderWithObj:@"1000000" andKeyValue:@"weight,gte"];
 [sem queryBuilderWithObj:@"1500000" andKeyValue:@"weight,lt"];
 [sem queryBuilderWithObj:@"newegg.com"andKeyValue:@"sitedetails,name" ];
 [sem queryBuilderWithObj:@"USD" andKeyValue:@"sitedetails,latestoffers,currency"];
 [sem queryBuilderWithObj:@"100" andKeyValue:@"sitedetails,latestoffers,price,gte"];

它不是固定的,但这只是一个示例。

4

1 回答 1

1

让你拥有的两个字典是 dict1 和 dict2

NSMutableDictionary* product1 = [dict1 objectForKey: @"products"];  //dict1 = {products = {  sitedetails =   { currency = USD; };}

NSMutableDictionary* product2 = [dict2 objectForKey: @"products"];   //dict2 = {products =    { sitedetails =   { latestoffers =   { price =  { test = { gte = 100; };  }; };  }; };  }

NSMutableDictionary* product1_sidetails = [product1 objectForKey: @"sidetails"];

NSMutableDictionary* product2_sidetails = [product2 objectForKey: @"sidetails"];

[product1_sidetails addEntriesFromDictionary:product2_sidetails ];

NSMutableDictionary* products = [[NSMutableDictionary alloc] init];
[products setObject: product1_sidetails forKey: @"sidetails"];

NSMutableDictionary* finalProducts = [[NSMutableDictionary alloc] init];
[finalProducts setObject:products forKey:@"products"];

//finalProducts will be your final dictionary
于 2013-11-01T06:15:09.527 回答