2013-02-14 117 views
0

我有一本字典,如:獲取NSDictionary的對象到數組中?

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

}

以上字典有四個按鍵即水平,marginavailable,marginutilised和淨利潤等

我想從第一三個按鍵和最後目標對象到一個數組。我嘗試了很多,但沒有找到任何邏輯來解析它。

我想解釋的陣列提前這個

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

這是否詞典包含詞典? – AppleDelegate 2013-02-14 09:31:27

回答

1

您可以使用NSMutableDictionaryaddEntriesFromDictionary:來構建一個新的詞典,用於合併原始數據的每個二級詞典。

事情是這樣的:

// Original data presumed held in origData 

NSMutableDictionary *newDict = [NSMutableDictionary dictionary]; 

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

// etc. for the other keys 
+0

感謝哥們,它真的幫了我很多 – Warewolf 2013-02-14 09:46:31

0

你只是嘗試下面的邏輯,它可以幫助你。

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

就像你從levels鍵添加所有對象,然後用同樣的方法爲marginavailablemarginutilised

然後最後加上net

[resultAry addObject:[parentDictionary objectForKey:@"net"]]; 
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]; 
     } 
     } 
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 :)

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