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.
问题:
回答1:
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
orenumerateObjectsUsingBlock
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.
回答2:
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.
回答3:
Have you looked at NSKeyValueCoding
? It has several methods you might find useful, including the following one, which can set all values on the object based on the dictionary representation of the object.
[object setValuesForKeysWithDictionary:dictionary];
Assuming you are able to determine the class of the object somehow from the dictionary representation, you can use this; also, you can turn objects into dictionaries quite easily using dictionaryWithValuesForKeys: