iOS:通常从NSObject类序列化/反序列化复杂的JSON [英] iOS: Serialize/Deserialize complex JSON generically from NSObject class

查看:193
本文介绍了iOS:通常从NSObject类序列化/反序列化复杂的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何序列化基于NSObject类的嵌套JSON?有一个讨论序列化简单的JSON 这里,但它不是通用的,足以应付复杂的嵌套JSON。



想象一下这是JSON的结果:

  {accounting:[{firstName:John,
lastName:Doe,
age:23},

{firstName:Mary,
lastName:Smith,
age:32}
],
sales:[{firstName :Sally,
lastName:绿色,
年龄:27},

{firstName:Jim,
lastName:Galley,
age:41}
]}

从这个类中:
$ b $ pre $ @interface Person:NSObject {}
@property(nonatomic,strong)NSString * firstName ;
@property(nonatomic,strong)NSString * lastName;
@property(nonatomic,strong)NSNumber * age;
@end

@interface部门:NSObject {}
@property(nonatomic,strong)NSMutableArray * accounting; //包含Person类
@property(nonatomic,strong)NSMutableArray * sales; //包含Person类
@end

如何基于类一般地序列化/反序列化它们?
$ b 编辑



目前我能够生成像这样的有效载荷在任何类:

  NSMutableDictionary * Payload = [self serialize:objClass]; 

但它并不适合嵌套的复杂JSON。任何人都有更好的解决方案? C#中的这个库 cater根据对象类序列化/反序列化。我希望根据NSObject重现相同的内容。

解决方案

最后,我们可以使用JSONModel 。这是迄今为止最好的方法。 JSONModel是一个通用的基于Class序列化/反序列化对象的库。你甚至可以使用基于non-nsobject的属性,如 int short float 。它也可以提供嵌套复杂的JSON。

1)反序列化示例。通过引用上面的例子,在头文件中:

  #importJSONModel.h

@接口Person:JSONModel
@property(非原子,强壮)NSString * firstName;
@property(nonatomic,strong)NSString * lastName;
@property(nonatomic,strong)NSNumber * age;
@end

@protocol Person;

@interface部门:JSONModel
@property(nonatomic,strong)NSMutableArray< Person> *会计;
@property(nonatomic,strong)NSMutableArray< Person> *销售;
@end

在执行文件中:

  #importJSONModelLib.h
#importmyJSONClass.h

NSString * responseJSON = / * from example * /;
Department * department = [[Department alloc] initWithString:responseJSON error:& err]; (@%@,person.firstName);
if
NSLog(@%@,person.lastName);
NSLog(@%@,person.age);


(Person @ person in department.sales){

NSLog(@%@,person.firstName);
NSLog(@%@,person.lastName);
NSLog(@%@,person.age);


$ c


2)序列化示例 。在执行文件中:

  #importJSONModelLib.h
#importmyJSONClass.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]);

这就是Serialize示例中的NSLog结果:

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


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 nested JSON.

Imagine this is the result of JSON:

{ "accounting" : [{ "firstName" : "John",  
                    "lastName"  : "Doe",
                    "age"       : 23 },

                  { "firstName" : "Mary",  
                    "lastName"  : "Smith",
                    "age"       : 32 }
                              ],                            
  "sales"      : [{ "firstName" : "Sally", 
                    "lastName"  : "Green",
                    "age"       : 27 },

                  { "firstName" : "Jim",   
                    "lastName"  : "Galley",
                    "age"       : 41 }
                  ]}

From this class:

@interface Person : NSObject{}
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSNumber *age;
@end

@interface Department : NSObject{}
@property (nonatomic, strong) NSMutableArray *accounting; //contain Person class
@property (nonatomic, strong) NSMutableArray *sales; //contain Person class
@end

How to serialize/deserialize them based on class generically?

EDIT

Currently i'm able to generate payload like this based on any class:

NSMutableDictionary *Payload = [self serialize:objClass];

But it does not cater nested complex JSON. Anyone have better solution for this? This library for C# cater serialize/deserialze based on object class. I want to reproduce something the same based on NSObject

解决方案

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<Person> *accounting;
@property (nonatomic, strong) NSMutableArray<Person> *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 }
                  ]}

这篇关于iOS:通常从NSObject类序列化/反序列化复杂的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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