任何人都知道如何基于 NSObject 类序列化嵌套的 JSON?这里有一个关于序列化简单 JSON 的讨论,但它不够通用,无法满足复杂的嵌套 JSON。
想象一下这是 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 }
]}
从这个类:
@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
一般如何基于类对它们进行序列化/反序列化?
编辑
目前我能够基于任何类生成这样的有效负载:
NSMutableDictionary *Payload = [self serialize:objClass];
但它不适合嵌套的复杂 JSON。有人对此有更好的解决方案吗?这个C# 库基于对象类满足序列化/反序列化。我想基于 NSObject 重现相同的东西