Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Model Classes(NSObject) automatically from json dictionary?

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.

like image 297
Vvk Avatar asked Mar 09 '16 12:03

Vvk


2 Answers

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).

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

  • Select Output language and add base Class name.(Class Prefix Not Required)
  • See Below Generated Model Classes and SubModel Classe (It is Autometicaly Generate All JSON Object Classes)


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 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];

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

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

like image 62
Vvk Avatar answered Oct 04 '22 10:10

Vvk


You can use modules like BaseModel, ObjectMapper or EVReflection open source projects for this purpose. They are all open source modules that you can use in your project.

like image 45
Pradeep K Avatar answered Oct 04 '22 09:10

Pradeep K