如何从json字典自动创建模型类(NSObject)? [英] How to create Model Classes(NSObject) automatically from json dictionary?

查看:172
本文介绍了如何从json字典自动创建模型类(NSObject)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法创建字典或json响应的模型类(包装器)?因为在我的应用程序中有许多web服务,所有WS都包含大数据。如果我一直在创建,那么创建NSObject类需要花费很多时间来检查Null数据和Encode-Decode对象。或者请建议我手动创建所有NSObject是正确的方法吗?我不想解析直接字典。
谢谢。

is there any way to create model classes(wrapper) of dictionary or json responce? because in my app there are many webservices and all WS contains big data. if i am creating all the time one by one it takes much time to create NSObject Class with Checking Null data and Encode-Decode Object. either please suggest me it is right way to create all NSObject manually? i can't want to parse direct dictionary. Thanks.

推荐答案

最后我得到了非常简单快速的模型类生成器工具,用于 iOS和Android 两者。只需复制并粘贴您的响应并从工具中获取模型类。

finally i got very easy and fast Model Class generator tool for iOS and Android both. just copy and paste your responce and get Model Class from tool.


  • Download JSON Accelerator from AppStore (Mac AppStore).
  • Copy your JSON Responce from browser.(responce must be JSON Validated).

粘贴在Json Accelerator中>>单击生成并使用模型名保存。

paste in Json Accelerator >> Click Generate and Save With Your Modelname.



像这样使用>>>
例如你的JSON Respoce词典是


Use Like This >>> For Example Your JSON Respoce Dictionary is

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}




  • make Employee Array,然后创建Object with Created Model Classes,如下面的代码和你必须使用 For循环 enumerateObjectsUsingBlock 任何一个用于create,然后使用Add in Mutable Array。

    • make Employee Array and then create Object With Created Model Classes like below code and you have to use For loop or enumerateObjectsUsingBlock any one for create and then Add in Mutable Array.

      NSArray * arrTemp = [NSArray arrayWithArray:yourResponceDict [@employees]];
      NSMutableArray * myMutableArray = [NSMutableArray array];

      NSArray *arrTemp = [NSArray arrayWithArray:yourResponceDict[@"employees"]]; NSMutableArray *myMutableArray = [NSMutableArray array];

      [arrTemp enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          Employee *aObj = [Employee modelObjectWithDictionary:obj];
          [myMutableArray addObject:aObj];
      }];
      


    • 此工具非常简单,节省时间创建Json对象类以在开发中的任何位置解析数据。

      This Tool is Very easy and Time Saving For Creating Json Object Classes for parsing data anywhere in Development.

      这篇关于如何从json字典自动创建模型类(NSObject)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆