如何在 Objective-C iphone sdk 中迭代嵌套字典
您好,我有一个 json 字符串,已通过 JSON 框架转换为字典,我需要提取其内容。我怎样才能迭代到嵌套字典?我已经有了这段代码,可以让我查看字典:
NSDictionary *results = [responseString JSONValue];
NSMutableArray *catArray = [NSMutableArray array];
for (id key in results) {
NSLog(@"key: %@, value: %@", key, [results objectForKey:key]);
[catArray addObject:key];
NSString *cat = key;
}
有人可以提供一个示例,说明如何在不使用键名的情况下到达 dic 的所有级别吗?
dic的结果结构是这样的: http://www.freeimagehosting.net/uploads/ e7c020d697.png
替代文本 http://www.freeimagehosting.net/uploads/e7c020d697 .png
谢谢
ldj
Hi I have a json string converted unsing the JSON framework into a dictionary and I need to extract its content. How could I iterate to the nested dictionaries? I have already this code that allows me to see the dictionary:
NSDictionary *results = [responseString JSONValue];
NSMutableArray *catArray = [NSMutableArray array];
for (id key in results) {
NSLog(@"key: %@, value: %@", key, [results objectForKey:key]);
[catArray addObject:key];
NSString *cat = key;
}
Could someone provide a sample on how to get to all the levels of the dic not using the name of the keys?
The result structure of the dic is like this: http://www.freeimagehosting.net/uploads/e7c020d697.png
alt text http://www.freeimagehosting.net/uploads/e7c020d697.png
thanks
ldj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我尝试过递归,希望这能有所帮助。
PS 这将返回该键的第一次出现。
I have tried recursion and hope this could help.
P.S. This will return the first occurrence of the key.
我确信您现在已经解决了这个问题,但我发现编写一个递归实用程序类函数来递归记录字典的每个键很方便:
I'm sure you have solved this at this point, but I found it convenient to write a recursive utility class function that recursively logs every key of a dictionary:
类别的标题:
正文:用法
/验证:
}
The header of the category:
The body:
Usage/Validation:
}
您可能必须尝试递归函数。
这将列出所有键及其级别。
You probably have to try recursive function.
this will list all keys and its level.
尝试
希望有帮助。
编辑:
我不确定我是否理解你的结构正确,但我假设你的字典中有一些值,它们又是字典。
如果你想通过位于另一个字典内部的字典进行交互,你可以这样做:
当然,如果你知道所需字典的特定键,你可以检查它而不是检查该值是否是字典。但也许在这两种情况下检查一下并不是一个坏主意......
Try
Hope that helped.
EDIT:
i'm not sure if i understand your structure correct, but i'm asuming you have some values in your dictionairy which are again dictionairies.
if you want to interate through a dictionairy which is located inside another one you could do it like this:
Of couse if you know the specific key to your desired dictionairy, you can check for that instead of checking if the value is a dict. But maybe it's not a bad idea to check that in both cases...