从服务器得到的JSON数据解析成NSDictionary,通过递归遍历可以方便的把字典中的所有键值输出出来方便测试检查。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
- (
NSString
*)stringFormDict:(NSDictionary*)dict
{
NSMutableString
*str = [
NSMutableString
string
];
NSArray
*keys = [dict
allKeys
];
for
(
NSString
*key
in
keys) {
if
([[dict
objectForKey
:key]
isKindOfClass
:[
NSDictionary
class
]]) {
id
obj = [dict
objectForKey
:key];
[str
appendFormat
:
@"\n%@: %@"
,key,[
self
stringFormDict
:obj]];
}
else
if
([[dict
objectForKey
:key]
isKindOfClass
:[
NSArray
class
]]){
[str
appendFormat
:
@"\n%@:"
,key];
for
(
id
obj
in
[dict
objectForKey
:key]) {
[str
appendFormat
:
@"\n%@"
,[
self
stringFormDict
:obj]];
}
}
else
{
[str
appendFormat
:
@"\n%@: %@"
,key,[dict
objectForKey
:key]];
}
}
return
str;
}
|
本文介绍如何从服务器获取JSON数据并解析为NSDictionary,通过递归遍历输出字典中的所有键值,方便进行测试和检查。提供了一个自定义函数用于实现这一过程,包括处理嵌套字典和数组的复杂结构。
2485

被折叠的 条评论
为什么被折叠?



