ios接收java序列化对象_ios – 将自定义对象序列化为包含NSMutableArray的JSON

我正在尝试序列化我的Cart对象,其中包含一个NSMutableArray项目,但得到一个:

***由于未捕获的异常’NSInvalidArgumentException’而终止应用程序,原因:’JSON写入(Item)中的类型无效’

如果我理解这应该如何工作,我需要创建一个字典数组,以便NSJSONSerialization正常工作.那不是我在下面做的吗?

我的Cart.h:

@interface Cart : BaseObject

@property (nonatomic, strong) NSString *comp;

@property (nonatomic, strong) NSString *sono;

@property (nonatomic, strong) NSString *cust;

@property (nonatomic, strong) NSString *scus;

@property (nonatomic, strong) NSString *cnid;

@property (nonatomic, strong) NSString *dldt;

@property (nonatomic, strong) NSString *whse;

@property (nonatomic, strong) NSString *pono;

@property (nonatomic, strong) NSString *pon2;

@property (nonatomic, strong) NSString *emad;

@property (nonatomic, strong) NSString *pkin;

@property (nonatomic, strong) NSString *comt;

@property (nonatomic, strong) NSString *rtin;

@property (nonatomic, strong) NSString *lbfg;

@property (nonatomic, strong) NSString *containsOpenPriced;

@property (nonatomic, strong) NSString *totalProductAmount;

@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic) BOOL *isSubmitting;

@end

我的购物车:

@implementation Cart

@synthesize comp;

@synthesize sono;

@synthesize cust;

@synthesize scus;

@synthesize cnid;

@synthesize dldt;

@synthesize whse;

@synthesize pono;

@synthesize pon2;

@synthesize emad;

@synthesize pkin;

@synthesize comt;

@synthesize rtin;

@synthesize lbfg;

@synthesize containsOpenPriced;

@synthesize totalProductAmount;

@synthesize items;

- (id) initWithDictionary:(NSDictionary *)dictionary {

self = [super init];

if (self) {

self.comp = dictionary[@"comp"];

self.sono = dictionary[@"sono"];

self.cust = dictionary[@"cust"];

self.scus = dictionary[@"scus"];

self.cnid = dictionary[@"cnid"];

self.dldt = dictionary[@"dldt"];

self.whse = dictionary[@"whse"];

self.pono = dictionary[@"pono"];

self.pon2 = dictionary[@"pon2"];

self.emad = dictionary[@"emad"];

self.pkin = dictionary[@"pkin"];

self.comt = dictionary[@"comt"];

self.rtin = dictionary[@"rtin"];

self.lbfg = dictionary[@"lbfg"];

self.containsOpenPriced = dictionary[@"containsOpenPriced"];

self.totalProductAmount = dictionary[@"totalProductAmount"];

NSArray *itemsArray = dictionary[@"items"];

NSMutableArray *itemsMutableArray = [[NSMutableArray alloc]init];

for (NSDictionary *itemDictionary in itemsArray) {

Item *item = [[Item alloc] initWithDictionary:itemDictionary];

[itemsMutableArray addObject:item];

}

self.items = itemsMutableArray;

}

return self;

}

@end

我的序列化对象的代码:

NSMutableArray *itemsToSerialize = [[NSMutableArray alloc] init];

for (Item *item in cart.items) {

NSMutableDictionary *itemDict = [[NSMutableDictionary alloc] init];

[itemDict setObject:item forKey:@"item"];

[itemsToSerialize addObject:item];

}

NSMutableDictionary *data = [@{

@"comp" : cart.comp,

@"rtin" : cart.rtin,

@"pono" : cart.pono,

@"pon2" : cart.pon2,

@"totalProductAmount" : totalProductAmount,

@"sono" : cart.sono,

@"emad" : cart.emad,

@"lbfg" : lbfg,

@"pkin" : cart.pkin,

@"containsOpenPriced" : containsOpenPriced,

@"cnid" : cart.cnid,

@"whse" : cart.whse,

@"scus" : cart.scus,

@"dldt" : cart.dldt,

@"cust" : cart.cust,

@"comt" : cart.comt,

@"items": itemsToSerialize

} mutableCopy];

NSString *command = @"shoppingCart.update";

NSMutableDictionary *request = [@{

@"command" : command,

@"comp" : cart.comp,

@"cnid" : sessionController.operator.cnid,

@"cust" : cart.cust,

@"data" : data

} mutableCopy];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:request options:kNilOptions error:nil];

这死在上面的NSJSONSerialization线上.我错过了什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值