【发布时间】:2012-10-31 15:15:59
【问题描述】:
我有一种情况,我从服务器获取数据作为 xml,我使用 https://github.com/Insert-Witty-Name 将该数据转换为 NSDictionary
我的xml就像
<Game>
<userid></userid>
<name></name>
<friendId></friendId>
<friendName></friendName>
</Game>
值 Game 和所有其他项目都可以改变,所以首先我需要找出第一个游戏元素的名称 =>
我通过上面的库转换了这个,现在我得到了
xmlString = [NSString stringWithFormat:@"<Game><userid>12</userid><userName>ben</userName><friendId>41</friendId><friendName>Ben1</friendName></Game>"];
NSDictionary *dict = [XMLReader dictionaryForXMLString:xmlString error:nil];
NSString *string;
string = [dict objectForKey:@"Game"];
NSLog(@"name is :%@",string);
输出是
{
friendId = 41;
friendName = Ben1;
userName = ben;
userid = 12;
}
我需要找出 xml 的根元素名称以及如何使用 objectForKey 在 NSDictionary 中访问这些值
【问题讨论】:
-
希望这篇文章对stackoverflow.com/a/1549311/857865有帮助
-
你能解释一下你的问题吗?
-
尝试字符串 = [[dict objectForKey:@"Game"] objectForKey:@"userName"];
-
-
我认为这里发生的事情是创建了嵌套字典,它的作用与 NSDictionary 相同
标签: iphone objective-c nsdictionary