#import "Mantle.h"
@interface Product : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy) NSNumber *rid;
@property (nonatomic, strong) NSNumber *price;
@end
@implementation Product
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"rid": @"id",
@"price": @"price",
};
}
@end
@interface Test : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSArray *products;
@end
@implementation Test
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"name": @"name",
@"products": @"products",
};
}
+ (NSValueTransformer *)productsJSONTransformer {
return [MTLJSONAdapter arrayTransformerWithModelClass:Product.class];
}
@end
//使用
NSDictionary *dic = @{
@"name": @"Ansel",
@"products":@[@{@"id" : @(1), @"price" : @(3)}, @{@"id" : @(2), @"price" : @(4)}],
};
Test *test = [MTLJSONAdapter modelOfClass:[Test class] fromJSONDictionary:dic error:nil];