iOS: Serialize/Deserialize complex JSON generically from NSObject class

后端 未结 3 708
栀梦
栀梦 2020-11-27 07:45

Anyone have idea how to serialize nested JSON based on NSObject class? There is a discussion to serialize simple JSON here , but it is not generic enough to cater complex n

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 08:17

    Finally we can solve this problem easily using JSONModel. This is the best method so far. JSONModel is a library that generically serialize/deserialize your object based on Class. You can even use non-nsobject based for property like int, short and float. It can also cater nested-complex JSON.

    1) Deserialize example. By referring to above example, in header file:

    #import "JSONModel.h"
    
    @interface Person : JSONModel 
    @property (nonatomic, strong) NSString *firstName;
    @property (nonatomic, strong) NSString *lastName;
    @property (nonatomic, strong) NSNumber *age;
    @end
    
    @protocol Person;
    
    @interface Department : JSONModel
    @property (nonatomic, strong) NSMutableArray *accounting;
    @property (nonatomic, strong) NSMutableArray *sales;
    @end
    

    in implementation file:

    #import "JSONModelLib.h"
    #import "myJSONClass.h"
    
    NSString *responseJSON = /*from example*/;
    Department *department = [[Department alloc] initWithString:responseJSON error:&err];
    if (!err)
    {
        for (Person *person in department.accounting) {
    
            NSLog(@"%@", person.firstName);
            NSLog(@"%@", person.lastName);
            NSLog(@"%@", person.age);         
        }
    
        for (Person *person in department.sales) {
    
            NSLog(@"%@", person.firstName);
            NSLog(@"%@", person.lastName);
            NSLog(@"%@", person.age);         
        }
    }
    

    2) Serialize Example. In implementation file:

    #import "JSONModelLib.h"
    #import "myJSONClass.h"
    
    Department *department = [[Department alloc] init];
    
    Person *personAcc1 = [[Person alloc] init];
    personAcc1.firstName = @"Uee";
    personAcc1.lastName = @"Bae";
    personAcc1.age = [NSNumber numberWithInt:22];
    [department.accounting addOject:personAcc1];
    
    Person *personSales1 = [[Person alloc] init];
    personSales1.firstName = @"Sara";
    personSales1.lastName = @"Jung";
    personSales1.age = [NSNumber numberWithInt:20];
    [department.sales addOject:personSales1];
    
    NSLog(@"%@", [department toJSONString]);
    

    And this is NSLog result from Serialize example:

    { "accounting" : [{ "firstName" : "Uee",  
                        "lastName"  : "Bae",
                        "age"       : 22 }
                     ],                            
      "sales"      : [{ "firstName" : "Sara", 
                        "lastName"  : "Jung",
                        "age"       : 20 }
                      ]}
    

提交回复
热议问题