0

我有一本像这样的字典:

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

}

上面的字典包含四个键,即levels、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";
};

提前致谢。

4

5 回答 5

1

您可以使用addEntriesFromDictionary:NSMutableDictionary构建一个新字典,将原始数据的每个二级字典合并。

像这样的东西:

// Original data presumed held in origData

NSMutableDictionary *newDict = [NSMutableDictionary dictionary];

[newDict addEntriesFromDictionary: [origData objectForKey: @"levels"]];

// etc. for the other keys
于 2013-02-14T09:27:48.123 回答
0

您只需尝试遵循逻辑它可能会对您有所帮助。

resultAry = [[NSMutableArray alloc]init];
[resultAry addObject:[[parentDictionary objectForKey:@"levels"]objectForKey:@"exchange"]];

就像你从levels键添加所有对象然后按照相同的方式marginavailablemarginutilised

然后最后添加net

[resultAry addObject:[parentDictionary objectForKey:@"net"]];
于 2013-02-14T09:29:51.857 回答
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];
        }
      }
于 2013-02-14T09:41:01.067 回答
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 中得到结果 :)

于 2013-02-14T09:47:40.903 回答
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];
    }];
于 2013-02-14T09:48:12.650 回答