将自定义对象序列化为包含NSMutableArray的JSON

我想序列化我的Cart对象,它有一个NSMutableArray的项目,但得到一个:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Item)'

如果我理解这应该如何工作,我需要创build一个字典数组,以便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 

我的Cart.m:

 @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行。 我错过了什么?

这一行: [itemsToSerialize addObject:item]; 应该是[itemsToSerialize addObject:itemDict]; 。 结果就是你试图序列化一些项目本身,这给你看到的exception。

NSJSONSerialization只适用于数组( NSArrayNSMutableArray ),字典( NSDictionaryNSMutableDictionary ,string( NSStringNSMutableString )和NSNumber

常见的机制是在你的类上创build一个- (NSDictionary *)serialize方法,将它的所有值复制到一个字典中传递给NSJSONSerialization。 然后实现- (id)initFromSerialization:(NSDictionary *)serialization来反- (id)initFromSerialization:(NSDictionary *)serialization对象。

我知道这是比较晚,但也许这个类可以减轻你的编码:

它是一个将Custom NSObject转换为JSON可读对象(NSArray或NSDictionary)的类。 试试看。 它有能力跳过属性和改变属性types从string到NSInteger或Float,它也可以改变最终的输出属性名称可以说

 CustomObject *X; X.property1 = @"Test"; //and the output JSON readable format you want tot change X to Y //CustomObject converted to readable format {Y = @"Test"} 

这里是反汇编程序