JSON转NSDictionary后遍历多维的NSDictionary

从服务器得到的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后遍历多维的NSDictionary)