我正在解析xml文件并在字典中获取值,我试图通过它们的名称提取值,但其中一些我得到和剩下的是null。这是我的字典,
dictionary: {
"__name" = QF;
"_category" = "C&M";
"_id" = AB2001;
"_ni_exempt" = no;
"_topic" = Alertness;
answers = {
answer = (
{
"_correct" = no;
text = "give an arm signal as well as using your indicators";
},
{
"_correct" = no;
text = "signal so that other drivers can slow down for you";
},
{
"_correct" = yes;
text = "look over your shoulder for a final check";
},
{
"_correct" = no;
text = "select a higher gear than normal";
}
);
};
question = {
explanation = {
text = "If you want to make a U-turn, slow down and ensure that the road is clear in both directions. Make sure that the road is wide enough to carry out the manoeuvre safely.";
voice = {
"_id" = "AB2001-2";
};
};
prompt = "Mark one answer";
text = "Before you make a U-turn in the road, you should";
voice = {
"_id" = "AB2001-1";
};
xref = "DES s4, DES s9, HC r159-161";
};
}
这是我试图提取值的代码,
-(void)fetchValuesFromDictioanry:(NSDictionary*)xmlDict
{
for (NSString *string in [xmlDict allKeys])
{
if ([[xmlDict valueForKey:string] isKindOfClass:[NSDictionary class]])
{
[self fetchValuesFromDictioanry:[xmlDict valueForKey:string]];
}
else if ([[xmlDict valueForKey:string] isKindOfClass:[NSString class]])
{
if ([string isEqualToString:@"_id"])
{
_lblID.text = [xmlDict valueForKey:@"_id"];
NSLog(@"ZZZ %@",_lblID.text);
_lblAlert.text = [xmlDict objectForKey:@"_topic"];
NSLog(@"Make: %@ ",_lblAlert.text);
_lblxref.text = [xmlDict objectForKey:@"xref"];
NSLog(@"Make: %@ ",_lblxref.text);
_lbltext.text = [xmlDict objectForKey:@"text"];
NSLog(@"Model: %@ ", _lbltext.text);
_lblgraphic.text = [xmlDict objectForKey:@"explanation"];
NSLog(@"Model: %@ ",_lblgraphic.text);
_lblprompt.text = [xmlDict objectForKey:@"prompt"];
NSLog(@"Model: %@ ",_lblprompt.text);
_lblvoice.text = [xmlDict objectForKey:@"_id"];
NSLog(@"Model: %@ ", _lblvoice.text);
}
// NSLog(@"MMM: %@ ", [xmlDict valueForKey:string]);
//Set here other labels
}
else if ([[xmlDict valueForKey:string] isKindOfClass:[NSArray class]])
{
for (NSDictionary *dictionary in [xmlDict valueForKey:string]) {
[self fetchValuesFromDictioanry:dictionary];
}
}
}
}
现在,当我在lblID和lblAlert等标签中获取值时,它会向我显示值,但是当我尝试获取内部值时,它会显示为null,如lbltext或lblxref。我如何从内部字典中获取值?
答案 0 :(得分:0)
用下面的
替换函数fetchValuesFromDictioanry-(void)fetchValuesFromDictioanry:(NSDictionary*)xmlDict
{
if ([xmlDict isKindOfClass:[NSDictionary class]])
{
for (NSString *string in [xmlDict allKeys])
{
if ([[xmlDict valueForKey:string] isKindOfClass:[NSDictionary class]])
{
[self fetchValuesFromDictioanry:[xmlDict valueForKey:string]];
}
else if ([[xmlDict valueForKey:string] isKindOfClass:[NSString class]])
{
if ([string isEqualToString:@"_id"])
{
_lblID.text = [xmlDict valueForKey:string];
NSLog(@"ZZZ %@",_lblID.text);
}
else if ([string isEqualToString:@"_topic"])
{
_lblAlert.text = [xmlDict objectForKey:string];
NSLog(@"Make: %@ ",_lblAlert.text);
}
else if ([string isEqualToString:@"xref"])
{
_lblxref.text = [xmlDict objectForKey:string];
NSLog(@"Make: %@ ",_lblxref.text);
}
else if ([string isEqualToString:@"text"])
{
_lbltext.text = [xmlDict objectForKey:string];
NSLog(@"Model: %@ ", _lbltext.text);
}
else if ([string isEqualToString:@"explanation"])
{
_lblgraphic.text = [xmlDict objectForKey:string];
NSLog(@"Model: %@ ",_lblgraphic.text);
}
else if ([string isEqualToString:@"prompt"])
{
_lblprompt.text = [xmlDict objectForKey:string];
NSLog(@"Model: %@ ",_lblprompt.text);
}
else if ([string isEqualToString:@"_id"])
{
_lblvoice.text = [xmlDict objectForKey:string];
NSLog(@"Model: %@ ", _lblvoice.text);
}
}
else if ([[xmlDict valueForKey:string] isKindOfClass:[NSArray class]])
{
for (NSDictionary *dictionary in [xmlDict valueForKey:string]) {
[self fetchValuesFromDictioanry:dictionary];
}
}
}
}
else
{
NSLog(@"Not a dictionary")
}
}