我有一个字典/列表balance_sign_dict,我需要根据条件检索2个值。 源XML是;
<Value>
<Signature>-873</Signature>
<Amount>1501042000</Amount>
</Value>
我想从下面显示的dict中解析数据; 您能否告诉我如何解析条件数据(例如,如果我想要“签名”节点的“金额”节点的值为-873),即它应该返回给我1501042000
我想要一种解析下面字典中值的通用方法。
打印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;
},
以下是我现在正在尝试的内容;
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];
}
}
但由于某种原因,nodeChildArray的第二部分被删除了......即。 nodeContent = 1501042000; nodeName = Amount;
我不知道如何访问该部分。
请帮忙。
}
}
实际上这是我在备用跑步中获得的;
打印nodeChildArray的说明: {type = mutable dict,count = 2, entries =&gt; 0:{contents =“nodeName”} = {contents =“签名”} 1:{contents =“nodeContent”} = {contents =“ - 1507”} }
打印nodeChildArray的说明: {type = mutable dict,count = 2, entries =&gt; 0:{contents =“nodeName”} = {contents =“Amount”} 1:{contents =“nodeContent”} = {contents =“631000”} }
如何获取匹配签名值的金额值?
答案 0 :(得分:1)
NSArray
和NSDictionary
是我所熟悉的任何语言中最好的容器类。 NSDictionary
可以存储任何对象或基元,包括NSArray
,反之亦然。当我说词典时,我随意提到任何一个班级。
当您输出任何字典的描述(就像NSLog(@"%@",myDict);
一样简单)时,字典的全部内容就像XML(或者更准确地说是JSON)文档一样显示。很容易看到字典描述的内容,以确定其中包含的内容。()
表示NSArray
,{}
表示NSDictionary
。
// this is the beginning of an array.
(
// this is the beginning of index 0 of the array, an `NSDictionary`
{
// objectForKey:@"nodeChildArray"] isKindOfClass:[NSArray class]]
nodeChildArray = ( // inside nodeChildArray is another array
{ // index 0 of array is a dictionary with two keys
// objectForKey:@"nodeContent" == @"-873"
nodeContent = "-873";
// objectForKey:@"nodeName" == @"Signature"
nodeName = Signature;
}, // end of index 0
{ // index 1 of array
nodeContent = 1501042000;
nodeName = Amount;
} // end of index 1
); // end of nodeChildArray, an NSArray stored at objectForKey:@"nodeChildArray"
// this is the only other element in the dictionary, a string
// objectForKey:@"nodeName"] isKindOfClass:[NSString class]]
nodeName = Value;
}, // end of the dictionary containing nodeChildArray and nodeName
{ // objectAtIndex:1
nodeChildArray = (
{
nodeContent = "-1228";
nodeName = Signature;
},
{
nodeContent = 428586000;
nodeName = Amount;
}
);
nodeName = Value;
},
{ // objectAtIndex:2
nodeChildArray = (
{
nodeContent = "-1140";
nodeName = Signature;
},
{
nodeContent = 79370000;
nodeName = Amount;
}
);
nodeName = Value;
},
);
因此,您可以从查看NSDictionary
调试输出中看到它实际上是NSArray
。结构的顺序是NSArray->NSDictionary->NSArray->NSString <--> [[[[NSArray objectAtIndex:i] NSDictionary objectForKey:@"nodeChildArray"] NSArray objectAtIndex:j] NSDictionary objectForKey:@"nodeContent"]
,最后返回NSString
。将此作为最新代码使用 - 在我将balance_sign_dict
作为NSDictionary
传递的代码的最后一个版本中。 这甚至可能有效!但是正确地施放论证是更好的形式。如果您不知道您的词典是以NSArray
还是NSDictionary
开头,则可以将其转换为id
并使用{{1}检查其类型}。请勿使用[theIdObject isKindOfClass:[{NSArray,NSDictionary} class]]
并在代码中选择其中一个。
{}