NSMutableDictionary+Json.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // NSMutableDictionary+Json.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 14-1-16.
  6. // Copyright (c) 2014年 CommonLibrary. All rights reserved.
  7. //
  8. #import "NSMutableDictionary+Json.h"
  9. @implementation NSMutableDictionary (Json)
  10. - (void)addString:(NSString *)aValue forKey:(id <NSCopying>)aKey
  11. {
  12. if (aValue == nil)
  13. {
  14. [self setObject:@"" forKey:aKey];
  15. }
  16. else
  17. {
  18. [self setObject:aValue forKey:aKey];
  19. }
  20. }
  21. - (void)addInteger:(NSInteger)aValue forKey:(id <NSCopying>)aKey
  22. {
  23. [self addNumber:[NSNumber numberWithInteger:aValue] forKey:aKey];
  24. }
  25. - (void)addCGFloat:(CGFloat)aValue forKey:(id <NSCopying>)aKey
  26. {
  27. [self addNumber:[NSNumber numberWithDouble:aValue] forKey:aKey];
  28. }
  29. - (void)addBOOL:(BOOL)aValue forKey:(id <NSCopying>)aKey
  30. {
  31. // [self addString:aValue ? @"Y" : @"N" forKey:aKey];
  32. [self addNumber:[NSNumber numberWithBool:aValue] forKey:aKey];
  33. }
  34. - (void)addBOOLStr:(BOOL)aValue forKey:(id <NSCopying>)aKey
  35. {
  36. [self addString:aValue ? @"Y" : @"N" forKey:aKey];
  37. }
  38. - (void)addNumber:(NSNumber *)aValue forKey:(id <NSCopying>)aKey
  39. {
  40. [self setObject:aValue forKey:aKey];
  41. }
  42. - (void)addArray:(NSArray *)aValue forKey:(id <NSCopying>)aKey
  43. {
  44. if (aValue == nil) {
  45. [self setObject:[NSNull null] forKey:aKey];
  46. }
  47. else
  48. {
  49. [self setObject:aValue forKey:aKey];
  50. }
  51. }
  52. - (NSString *)convertToJSONString
  53. {
  54. if ([NSJSONSerialization isValidJSONObject:self])
  55. {
  56. NSError *error = nil;
  57. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error: &error];
  58. NSString *string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  59. return string;
  60. }
  61. return nil;
  62. }
  63. @end
  64. @implementation NSDictionary (Json)
  65. - (id)jsonObjectForKey:(id<NSCopying>)key
  66. {
  67. id value = [self objectForKey:key];
  68. if ([value isKindOfClass:[NSNull class]]) {
  69. return nil;
  70. }
  71. return value;
  72. }
  73. - (NSMutableDictionary *)dictionaryForKey:(id<NSCopying>)key
  74. {
  75. NSDictionary *dic = (NSDictionary *)[self jsonObjectForKey:key];
  76. return [NSMutableDictionary dictionaryWithDictionary:dic];
  77. }
  78. - (NSString *)stringForKey:(id<NSCopying>)key
  79. {
  80. return [self jsonObjectForKey:key];
  81. }
  82. - (NSNumber *)numberForKey:(id<NSCopying>)key
  83. {
  84. NSNumber *num = [self jsonObjectForKey:key];
  85. return num;
  86. }
  87. - (NSInteger)integerForKey:(id<NSCopying>)key
  88. {
  89. return [self numberForKey:key].integerValue;
  90. }
  91. - (BOOL)boolForKey:(id<NSCopying>)key
  92. {
  93. id va = [self jsonObjectForKey:key];
  94. if (va)
  95. {
  96. if ([va isKindOfClass:[NSString class]])
  97. {
  98. NSString *value = (NSString *)va;
  99. return [[value lowercaseString] isEqualToString:@"y"];
  100. }
  101. else
  102. {
  103. return [self numberForKey:key].boolValue;
  104. }
  105. }
  106. return NO;
  107. }
  108. - (CGFloat)floatForKey:(id<NSCopying>)key
  109. {
  110. return [self numberForKey:key].floatValue;
  111. }
  112. - (double)doubleForKey:(id<NSCopying>)key
  113. {
  114. return [self numberForKey:key].doubleValue;
  115. }
  116. - (NSMutableArray *)arrayForKey:(id<NSCopying>)key
  117. {
  118. NSArray *array = [self jsonObjectForKey:key];
  119. NSMutableArray *mutablearray = [NSMutableArray array];
  120. for (NSDictionary *dic in array) {
  121. NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:dic];
  122. [mutablearray addObject:mdic];
  123. }
  124. return mutablearray;
  125. }
  126. @end