我有一个json对象,其中包含对象数组的数组。 我需要使用JSONModel解析它。
例如json:
{
"object": [
[
{
"a": 1,
"b": 2
},
{
"c": 1,
"d": 4
}
],
[
{
"a": 3,
"b": 6
},
{
"e": 2,
"f": 3
}
]
]
}
我应该如何在我的JSONModel中声明它? 像这样声明给我作为NSDictionary的NSArray。
@interface SomeClass : JSONModel
@property (nonatomic, retain) NSArray<NSArray<AphabetJsonModelClass> *> *object;
@end
答案 0 :(得分:1)
您应该使用这样的自定义getter / setter。
@interface SomeClass : JSONModel
@property (nonatomic, copy) NSArray<Optional> *object;// NSArray<ABPlusCDModel *> *
@end
@implementation SomeClass
- (void)setObjectWithNSArray:(NSArray *)array {
NSMutableArray *list = [NSMutableArray arrayWithCapacity:array.count];
[array enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL *stop) {
if (![obj isKindOfClass:[NSArray class]]) {
return;
}
NSMutableArray *l = [NSMutableArray arrayWithCapacity:obj.count];
[obj enumerateObjectsUsingBlock:^(NSDictionary *o, NSUInteger idx, BOOL *stop) {
ABPlusCDModel *m = [[ABPlusCDModel alloc] initWithDictionary:o error:nil];
if (m) {
[l addObject:m];
}
}];
[list addObject:l.copy];
}];
self.object = list;
}
- (NSArray *)JSONObjectForObject {
NSMutableArray *list = [NSMutableArray arrayWithCapacity:self.object.count];
[self.group enumerateObjectsUsingBlock:^(NSArray *a, NSUInteger idx, BOOL *stop) {
NSMutableArray *l = [NSMutableArray arrayWithCapacity:a.count];
[a enumerateObjectsUsingBlock:^(ABPlusCDModel *o, NSUInteger idx, BOOL *stop) {
[l addObject:o.toDictionary];
}];
[list addObject:l];
}];
return list.copy;
}