JSON与MODEL互转

  1. //  
  2. //  HYBJSONModel.h  
  3. //  Json2ModelDemo  
  4. //  
  5. //  Created by huangyibiao on 14-9-15.  
  6. //  Copyright (c) 2014年 Home. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. /*! 
  12.  * @brief JSON转换成Model,或者把Model转换成JSON 
  13.  * @author huangyibiao 
  14.  */  
  15. @interface HYBJSONModel : NSObject  
  16.   
  17. /*! 
  18.  * @brief 把对象(Model)转换成字典 
  19.  * @param model 模型对象 
  20.  * @return 返回字典 
  21.  */  
  22. + (NSDictionary *)dictionaryWithModel:(id)model;  
  23.   
  24. /*! 
  25.  * @brief 获取Model的所有属性名称 
  26.  * @param model 模型对象 
  27.  * @return 返回模型中的所有属性值 
  28.  */  
  29. + (NSArray *)propertiesInModel:(id)model;  
  30.   
  31. /*! 
  32.  * @brief 把字典转换成模型,模型类名为className 
  33.  * @param dict 字典对象 
  34.  * @param className 类名 
  35.  * @return 返回数据模型对象 
  36.  */  
  37. + (id)modelWithDict:(NSDictionary *)dict className:(NSString *)className;  
  38.   
  39. @end  


[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  HYBJSONModel.m  
  3. //  Json2ModelDemo  
  4. //  
  5. //  Created by huangyibiao on 14-9-15.  
  6. //  Copyright (c) 2014年 Home. All rights reserved.  
  7. //  
  8.   
  9. #import "HYBJSONModel.h"  
  10. #import <objc/runtime.h>  
  11.   
  12. typedef NS_ENUM(NSInteger, HYBJSONModelDataType) {  
  13.     kHYBJSONModelDataTypeObject    = 0,  
  14.     kHYBJSONModelDataTypeBOOL      = 1,  
  15.     kHYBJSONModelDataTypeInteger   = 2,  
  16.     kHYBJSONModelDataTypeFloat     = 3,  
  17.     kHYBJSONModelDataTypeDouble    = 4,  
  18.     kHYBJSONModelDataTypeLong      = 5,  
  19. };  
  20.   
  21. @implementation HYBJSONModel  
  22.   
  23. /*! 
  24.  * @brief 把对象(Model)转换成字典 
  25.  * @param model 模型对象 
  26.  * @return 返回字典 
  27.  */  
  28. + (NSDictionary *)dictionaryWithModel:(id)model {  
  29.     if (model == nil) {  
  30.         return nil;  
  31.     }  
  32.       
  33.     NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];  
  34.       
  35.     // 获取类名/根据类名获取类对象  
  36.     NSString *className = NSStringFromClass([model class]);  
  37.     id classObject = objc_getClass([className UTF8String]);  
  38.       
  39.     // 获取所有属性  
  40.     unsigned int count = 0;  
  41.     objc_property_t *properties = class_copyPropertyList(classObject, &count);  
  42.       
  43.     // 遍历所有属性  
  44.     for (int i = 0; i < count; i++) {  
  45.         // 取得属性  
  46.         objc_property_t property = properties[i];  
  47.         // 取得属性名  
  48.         NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)  
  49.                                                           encoding:NSUTF8StringEncoding];  
  50.         // 取得属性值  
  51.         id propertyValue = nil;  
  52.         id valueObject = [model valueForKey:propertyName];  
  53.           
  54.         if ([valueObject isKindOfClass:[NSDictionary class]]) {  
  55.             propertyValue = [NSDictionary dictionaryWithDictionary:valueObject];  
  56.         } else if ([valueObject isKindOfClass:[NSArray class]]) {  
  57.             propertyValue = [NSArray arrayWithArray:valueObject];  
  58.         } else {  
  59.             propertyValue = [NSString stringWithFormat:@"%@", [model valueForKey:propertyName]];  
  60.         }  
  61.           
  62.         [dict setObject:propertyValue forKey:propertyName];  
  63.     }  
  64.     return [dict copy];  
  65. }  
  66.   
  67. /*! 
  68.  * @brief 获取Model的所有属性名称 
  69.  * @param model 模型对象 
  70.  * @return 返回模型中的所有属性值 
  71.  */  
  72. + (NSArray *)propertiesInModel:(id)model {  
  73.     if (model == nil) {  
  74.         return nil;  
  75.     }  
  76.       
  77.     NSMutableArray *propertiesArray = [[NSMutableArray alloc] init];  
  78.       
  79.     NSString *className = NSStringFromClass([model class]);  
  80.     id classObject = objc_getClass([className UTF8String]);  
  81.     unsigned int count = 0;  
  82.     objc_property_t *properties = class_copyPropertyList(classObject, &count);  
  83.       
  84.     for (int i = 0; i < count; i++) {  
  85.         // 取得属性名  
  86.         objc_property_t property = properties[i];  
  87.         NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)  
  88.                                                           encoding:NSUTF8StringEncoding];  
  89.         [propertiesArray addObject:propertyName];  
  90.     }  
  91.       
  92.     return [propertiesArray copy];  
  93. }  
  94.   
  95. /*! 
  96.  * @brief 把字典转换成模型,模型类名为className 
  97.  * @param dict 字典对象 
  98.  * @param className 类名 
  99.  * @return 返回数据模型对象 
  100.  */  
  101. + (id)modelWithDict:(NSDictionary *)dict className:(NSString *)className {  
  102.     if (dict == nil || className == nil || className.length == 0) {  
  103.         return nil;  
  104.     }  
  105.       
  106.     id model = [[NSClassFromString(className) alloc]init];  
  107.       
  108.     // 取得类对象  
  109.     id classObject = objc_getClass([className UTF8String]);  
  110.       
  111.     unsigned int count = 0;  
  112.     objc_property_t *properties = class_copyPropertyList(classObject, &count);  
  113.     Ivar *ivars = class_copyIvarList(classObject, nil);  
  114.   
  115.     for (int i = 0; i < count; i ++) {  
  116.         // 取得成员名  
  117.         NSString *memberName = [NSString stringWithUTF8String:ivar_getName(ivars[i])];  
  118.         const charchar *type = ivar_getTypeEncoding(ivars[i]);  
  119.         NSString *dataType =  [NSString stringWithCString:type encoding:NSUTF8StringEncoding];  
  120.           
  121.         NSLog(@"Data %@ type: %@",memberName,dataType);  
  122.           
  123.         HYBJSONModelDataType rtype = kHYBJSONModelDataTypeObject;  
  124.         if ([dataType hasPrefix:@"c"]) {  
  125.             rtype = kHYBJSONModelDataTypeBOOL;// BOOL  
  126.         } else if ([dataType hasPrefix:@"i"]) {  
  127.             rtype = kHYBJSONModelDataTypeInteger;// int  
  128.         } else if ([dataType hasPrefix:@"f"]) {  
  129.             rtype = kHYBJSONModelDataTypeFloat;// float  
  130.         } else if ([dataType hasPrefix:@"d"]) {  
  131.             rtype = kHYBJSONModelDataTypeDouble; // double  
  132.         } else if ([dataType hasPrefix:@"l"])  {  
  133.             rtype = kHYBJSONModelDataTypeLong;// long  
  134.         }  
  135.           
  136.         for (int j = 0; j < count; j++) {  
  137.             objc_property_t property = properties[j];  
  138.             NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property)  
  139.                                                               encoding:NSUTF8StringEncoding];  
  140.             NSRange range = [memberName rangeOfString:propertyName];  
  141.               
  142.             if (range.location == NSNotFound) {  
  143.                 continue;  
  144.             } else {  
  145.                 id propertyValue = [dict objectForKey:propertyName];  
  146.                   
  147.                 switch (rtype) {  
  148.                     case kHYBJSONModelDataTypeBOOL: {  
  149.                         BOOL temp = [[NSString stringWithFormat:@"%@", propertyValue] boolValue];  
  150.                         propertyValue = [NSNumber numberWithBool:temp];  
  151.                     }  
  152.                         break;  
  153.                     case kHYBJSONModelDataTypeInteger: {  
  154.                         int temp = [[NSString stringWithFormat:@"%@", propertyValue] intValue];  
  155.                         propertyValue = [NSNumber numberWithInt:temp];  
  156.                     }  
  157.                         break;  
  158.                     case kHYBJSONModelDataTypeFloat: {  
  159.                         float temp = [[NSString stringWithFormat:@"%@", propertyValue] floatValue];  
  160.                         propertyValue = [NSNumber numberWithFloat:temp];  
  161.                     }  
  162.                         break;  
  163.                     case kHYBJSONModelDataTypeDouble: {  
  164.                         double temp = [[NSString stringWithFormat:@"%@", propertyValue] doubleValue];  
  165.                         propertyValue = [NSNumber numberWithDouble:temp];  
  166.                     }  
  167.                         break;  
  168.                     case kHYBJSONModelDataTypeLong: {  
  169.                         long long temp = [[NSString stringWithFormat:@"%@",propertyValue] longLongValue];  
  170.                         propertyValue = [NSNumber numberWithLongLong:temp];  
  171.                     }  
  172.                         break;  
  173.                           
  174.                     default:  
  175.                         break;  
  176.                 }  
  177.                   
  178.                 [model setValue:propertyValue forKey:memberName];  
  179.                 break;  
  180.             }  
  181.         }  
  182.     }  
  183.     return model;  
  184. }  
  185.   
  186. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值