你在把字典转成object的时候还在按下面这样:

self.id = [jsonDict objectForKey:@"id"];
self.name = [jsonDict objectForKey:@"name"];
self.profileImageBig = [jsonDict objectForKey:@"profile_image_big"];
self.profileImageSmall = [jsonDict objectForKey:@"profile_image_small"];
self.profileImageSquare = [jsonDict objectForKey:@"profile_image_square"];
self.firstName = [jsonDict objectForKey:@"firstName"];
self.familyName = [jsonDict objectForKey:@"familyName"];
self.age = [jsonDict objectForKey:@"age"];

这样做你就out了,太繁琐,得判断值的nil,null,类型等。使用JSONModel这样即可搞定

@interface MyModel: JSONModel
@property (strong, nonatomic) NSString* id;
@property (strong, nonatomic) NSString* name;
(etc...)
@end

添加JSONModel

pod管理的直接pod 'JSONModel'

其它的直接去下载包:https://github.com/icanzilb/JSONModel

教程参考:

http://www.touch-code-magazine.com/JSONModel/

http://www.raywenderlich.com/12139/introduction-to-cocoapods

简单介绍几个方面

  1. 指定一个索引,例如:

    @property (strong, nonatomic) NSString<index>* name

    你看到有一个Index,作用就是可以直接在数组中查找符合条件的对象,例如查找数组中object的name是sharofat的对象可以像下面这样写:

    //查找index为sharofat的
    NSArray *loans = feed.loans;
    NSLog(@"modelWithIndexValue --->%@",[loans modelWithIndexValue:@"Sharofat"]);
  2. object的数组和dict的数组相互转换,objce转json,dict

    //将model的array转成dict的array
    NSMutableArray *dictArray = [LoanModel arrayOfDictionariesFromModels:feed.loans];
    NSLog(@"arrayOfDictionariesFromModels===>%@",dictArray); //将dict的array转成model的array
    NSMutableArray *modelArray = [LoanModel arrayOfModelsFromDictionaries:dictArray];
    NSLog(@"arrayOfModelsFromDictionaries===>%@",modelArray); LoanModel* loan = feed.loans[indexPath.row];
    NSLog(@"loan.toDictionary--->%@",loan.toDictionary);
    NSLog(@"loan.toJSONString--->%@",loan.toJSONString);
  3. json,dict转object时判断value

    -(BOOL)validate:(NSError**)err
    {
    if ([self.name isEqual:@"Winfred"]) {
    self.name = @"Winfred rewrite name";
    // return NO;
    }
    NSLog(@"Loan of %@", self.name);
    NSLog(@"sector of %@", self.modelSector);
    NSLog(@"plandate of %@", self.plandate);
    return YES;
    }
  4. 很重要的keyMapper,指定映射值,如果不指定就是默认的

    • 转换带下划线的,例如:user_name 转换对应的key就是userName


      +(JSONKeyMapper*)keyMapper
      {
      return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
      }
    • 自定义key,例如: planned_expiration_date转换想对应plandate

      +(JSONKeyMapper*)keyMapper
      {
      return [[JSONKeyMapper alloc] initWithJSONToModelBlock:^NSString *(NSString *keyName) { if ([keyName isEqual:@"planned_expiration_date"]) {
      return @"plandate";
      }else if ([keyName isEqual:@"sector"]) {
      return @"modelSector";
      }
      else {
      return keyName;
      } } modelToJSONBlock:^NSString *(NSString *keyName) { if ([keyName isEqual:@"plandate"]) {
      return @"planned_expiration_date";
      }else if ([keyName isEqual:@"modelSector"]) {
      return @"sector";
      }else {
      return keyName;
      } }];
      }

      也可以像下面这样写:

      +(JSONKeyMapper*)keyMapper {
      return [[JSONKeyMapper alloc]initWithDictionary:@{@"sector":@"modelSector"}];
      }
  5. 指定定义的key的类型

    • optional表示字段可选,例如

      //链接字段是可选的,转换的时候允许link为空
      @property (nonatomic,strong) NSString<optional> *link;
    • index表示索引,参照1

    • convertondemand转换对象数组,例如:

      //表示数组是LoanModel对象
      @property (strong, nonatomic) NSArray<loanmodel , ConvertOnDemand>* loans;

最新文章

  1. CF219D. Choosing Capital for Treeland [树形DP]
  2. 95、Jenkins部署.net持续集成自动化测试环境
  3. json处理总结(前端js和后端java)
  4. JSP 和 Servlet 有哪些相同点和不同点,他们之间的联系是什么?
  5. 移动端web之像素基础
  6. Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
  7. ASP.NET的分页方法(四)
  8. PHP中使用Luhn算法校验信用卡及借记卡卡号
  9. unidac连接FireBird数据库
  10. 【Hibernate步步为营】--映射合集汇总
  11. Java 多态,重载,重写
  12. 基于Angularjs实现图片上传和下载
  13. springmvc注解
  14. 了解git的命令行使用
  15. Visual Studio Code-批量在文末添加文本字段
  16. 一篇读懂HTTPS:加密原理、安全逻辑、数字证书等
  17. 初识Haskell
  18. Conversation function
  19. DBC格式解析(数据部分)
  20. Java复习总结——数据类型

热门文章

  1. plsql编程
  2. 使用CORS解决flask前端页面跨域问题
  3. 微信小程序--列表渲染
  4. ALPHA 冲刺(一)
  5. 将数据缓存到sessionStorage中
  6. BZOJ1566 [NOI2009]管道取珠 【dp】
  7. 使用filter: blur() 的时候解决图片周围泛白和容器外范围变模糊的问题
  8. 【CF1073A】Diverse Substring(签到)
  9. CODEVS【3556】科技庄园
  10. 用来武装Firebug的十四款Firefox插件