我有一本字典:
{
levels = {
exchange = {
text = ALL;
};
product = {
text = ALL;
};
segment = {
text = ALL;
};
symbol = {
text = ALL;
};
};
marginavailable = {
adhocmargin = {
text = "0.00";
};
branchadhoc = {
text = "0.00";
};
cashmarginavailable = {
text = "0.00";
};
collateralvalue = {
text = "0.00";
};
credits = {
text = "0.00";
};
directcollateralvalue = {
text = "0.00";
};
notionalcash = {
text = 0;
};
payinamount = {
text = "0.00";
};
};
marginutilised = {
adhocscripmargin = {
text = "0.00";
};
category = {
text = 0;
};
cncmarginused = {
text = 0;
};
cncsellcreditpresent = {
text = 0;
};
debits = {
text = "0.00";
};
elm = {
text = "0.00";
};
exposuremargin = {
text = "0.00";
};
grossexposurevalue = {
text = "0.00";
};
ipoamount = {
text = "0.00";
};
mfamount = {
text = "0.00";
};
multiplier = {
text = "0.00";
};
payoutamount = {
text = "0.00";
};
premiumpresent = {
text = "0.00";
};
realisedmtom = {
text = "-0.00";
};
scripbasketmargin = {
text = "0.00";
};
spanmargin = {
text = "0.00";
};
subtotal = {
text = "0.00";
};
turnover = {
text = "0.00";
};
unrealisedmtom = {
text = "-0.00";
};
valueindelivery = {
text = "0.0";
};
varmargin = {
text = "0.00";
};
};
net = {
text = "0.00";
};
}
上面的字典包含四个关键字:level,marginavailable,marginutilised和net等
我想要从前三个键和最后一个对象到数组的对象。我已经尝试了很多,但没有找到任何逻辑来解析它。
我想要这样的字典数组
exchange = {
text = ALL;
};
product = {
text = ALL;
};
segment = {
text = ALL;
};
symbol = {
text = ALL;
};
adhocmargin = {
text = "0.00";
};
branchadhoc = {
text = "0.00";
};
cashmarginavailable = {
text = "0.00";
};
collateralvalue = {
text = "0.00";
};
credits = {
text = "0.00";
};
directcollateralvalue = {
text = "0.00";
};
notionalcash = {
text = 0;
};
payinamount = {
text = "0.00";
};
adhocscripmargin = {
text = "0.00";
};
category = {
text = 0;
};
cncmarginused = {
text = 0;
};
cncsellcreditpresent = {
text = 0;
};
debits = {
text = "0.00";
};
elm = {
text = "0.00";
};
exposuremargin = {
text = "0.00";
};
grossexposurevalue = {
text = "0.00";
};
ipoamount = {
text = "0.00";
};
mfamount = {
text = "0.00";
};
multiplier = {
text = "0.00";
};
payoutamount = {
text = "0.00";
};
premiumpresent = {
text = "0.00";
};
realisedmtom = {
text = "-0.00";
};
scripbasketmargin = {
text = "0.00";
};
spanmargin = {
text = "0.00";
};
subtotal = {
text = "0.00";
};
turnover = {
text = "0.00";
};
unrealisedmtom = {
text = "-0.00";
};
valueindelivery = {
text = "0.0";
};
varmargin = {
text = "0.00";
};
net = {
text = "0.00";
};
提前感谢。
答案 0 :(得分:1)
您可以使用addEntriesFromDictionary:
NSMutableDictionary
来构建一个合并原始数据的每个二级词典的新词典。
这样的事情:
// Original data presumed held in origData
NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
[newDict addEntriesFromDictionary: [origData objectForKey: @"levels"]];
// etc. for the other keys
答案 1 :(得分:0)
您只需尝试遵循可能对您有帮助的逻辑。
resultAry = [[NSMutableArray alloc]init];
[resultAry addObject:[[parentDictionary objectForKey:@"levels"]objectForKey:@"exchange"]];
就像您添加levels
键中的所有对象一样,然后按照marginavailable
和marginutilised
然后最后添加net
[resultAry addObject:[parentDictionary objectForKey:@"net"]];
答案 2 :(得分:0)
如果maindictionary包含词典
,则此代码应保持良好状态 NSMutableArray *dictionaryArray = [[NSMutableArray alloc]init];
for (NSDictionary *childDictionaries in rootDict) { // rootDict is the original dictionary whic has all the four keys levels,marginAvailable,marginutilized and net
for (NSDictionary *dict in childDictionaries) {
[dictionaryArray addObject:dict];
}
}
答案 3 :(得分:0)
NSMutableArray *newArray = [[NSMutableArray alloc]init];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
if ([(NSString *)key isEqualToString:@"marginutilised"])
*stop = YES;
[newArray addObjectsFromArray:(NSArray *)obj];
}];
现在你可以在newArray中得到结果:)
答案 4 :(得分:0)
要获取数组中的所有对象,还可以使用enumerateKeysAndObjectsUsingBlock:像这样
NSMutableArray *yourArray = [NSMutableArray arrayWithCapacity:0];
[yourDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if([(NSString*)key isEqualToString:@"net"]){
[yourArray addObject:[(NSDictionary*)obj objectForKey:@"net"]];
*stop = YES;
}
[yourArray addObject:obj];
}];