I have a dictionary/list balance_sign_dict from which I need to retrieve 2 values based on condition.
The source XML for this is;
<Value>
<Signature>-873</Signature>
<Amount>1501042000</Amount>
</Value>
I want to parse data from the dict shown below;
Could you please tell me how do I parse conditional data (e.g. if I want the value of “Amount” node for “Signature” node having -873) i.e. it should return me 1501042000
I want a generic way of parsing the values from the dictionary below.
Printing description of balance_sign_dict:
(
{
nodeChildArray = (
{
nodeContent = "-873";
nodeName = Signature;
},
{
nodeContent = 1501042000;
nodeName = Amount;
}
);
nodeName = Value;
},
{
nodeChildArray = (
{
nodeContent = "-1228";
nodeName = Signature;
},
{
nodeContent = 428586000;
nodeName = Amount;
}
);
nodeName = Value;
},
{
nodeChildArray = (
{
nodeContent = "-1140";
nodeName = Signature;
},
{
nodeContent = 79370000;
nodeName = Amount;
}
);
nodeName = Value;
},
Here is what I am trying now;
for (NSDictionary *valueNode in balance_sign_dict){
for (NSArray *nodeChildArray in [valueNode objectForKey:@"nodeChildArray"]){
NSString *tmpNodeName = [nodeChildArray objectAtIndex:1];
NSDictionary *tmpD = [valueNode objectAtIndex:1];
if ([tmpNodeName isEqualToString:@"Signature"]) {
tmpSignVal = [nodeChildArray objectAtIndex:0];
if ([tmpSignVal isEqualToString:@"-873"]) {
tmpSignAmt = [nodeChildArray objectAtIndex:1];
}
}
But for some reason the 2nd part of nodeChildArray gets removed…i.e. nodeContent = 1501042000;
nodeName = Amount;
I am not sure how to access that part.
Please help.
}
}
Actually here is what i get during alternate runs;
Printing description of nodeChildArray:
{type = mutable dict, count = 2,
entries =>
0 : {contents = “nodeName”} = {contents = “Signature”}
1 : {contents = “nodeContent”} = {contents = “-1507”}
}
Printing description of nodeChildArray:
{type = mutable dict, count = 2,
entries =>
0 : {contents = “nodeName”} = {contents = “Amount”}
1 : {contents = “nodeContent”} = {contents = “631000”}
}
How do I get the Amount value for matching Signature value?
NSArrayandNSDictionaryare the best container classes that I know of in any language that I am experienced with.NSDictionarycan store any object or primitive, including anNSArray, and vice versa. When I say dictionary I am arbitrarily referring to either class.When you output the description of any dictionary (which is as simple as
NSLog(@"%@",myDict);, the entire contents of the dictionary are displayed like an XML (or maybe more accurately a JSON) document. It is easy to look at the content of the dictionary description to determine what is contained inside.()denotes anNSArrayand{}denotes anNSDictionary.So you can see from looking at your
NSDictionarydebug output that it’s actually anNSArray. The order of structures isNSArray->NSDictionary->NSArray->NSString <--> [[[[NSArray objectAtIndex:i] NSDictionary objectForKey:@"nodeChildArray"] NSArray objectAtIndex:j] NSDictionary objectForKey:@"nodeContent"], finally returning anNSString. Use this as the most recent code – in the last version of the code I passedbalance_sign_dictas anNSDictionary. This might have even worked! But it is much better form to cast the argument properly. If you don’t know whether or not your dictionary is going to start with anNSArrayor anNSDictionary, you can cast it as anidand check its type with[theIdObject isKindOfClass:[{NSArray,NSDictionary} class]]. Don’t use the{}and choose one of the two in your code.