Ios JSONModel:将模型集合添加到JSON并手动添加集合

Ios JSONModel:将模型集合添加到JSON并手动添加集合,ios,objective-c,jsonmodel,Ios,Objective C,Jsonmodel,假设我有这个模型 @protocol ProductModel @end @interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end @interfa

假设我有这个模型

@protocol ProductModel
@end

@interface ProductModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;
@end

@implementation ProductModel
@end

@interface OrderModel : JSONModel
@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) NSArray<ProductModel>* products;
@end

@implementation OrderModel
@end
我认为这可以非常巧妙地帮助您:

ProductModel *productModel1 = [[ProductModel alloc] init];
productModel1.id = 123;
productModel1.name = @"Product #1";
productModel1.price = 12.95;

ProductModel *productModel2 = [[ProductModel alloc] init];
productModel2.id = 137;
productModel2.name = @"Product #2";
productModel2.price = 82.95;

OrderModel *orderModel = [[OrderModel alloc] init];
orderModel.order_id = 104;
orderModel.total_price = 103.45;
orderModel.products = @[productModel1, productModel2];

NSString *producedJSON = [orderModel toJSONString];
ProductModel *productModel1 = [[ProductModel alloc] init];
productModel1.id = 123;
productModel1.name = @"Product #1";
productModel1.price = 12.95;

ProductModel *productModel2 = [[ProductModel alloc] init];
productModel2.id = 137;
productModel2.name = @"Product #2";
productModel2.price = 82.95;

OrderModel *orderModel = [[OrderModel alloc] init];
orderModel.order_id = 104;
orderModel.total_price = 103.45;
orderModel.products = @[productModel1, productModel2];

NSString *producedJSON = [orderModel toJSONString];