[英]Serialize custom object to JSON which contains NSMutableArray
我正在嘗試序列化我的Cart對象,該對象中包含項的NSMutableArray
,但得到:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Item)'
如果我了解這應該如何工作,那么我需要創建一個字典數組以使NSJSONSerialization
正確工作。 那不是我在下面做什么嗎?
我的購物車:
@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
行。 我想念什么?
這行: [itemsToSerialize addObject:item];
應該是[itemsToSerialize addObject:itemDict];
。 結果是您要嘗試序列化項本身的數組,從而產生您所看到的異常。
NSJSONSerialization僅適用於數組( NSArray
或NSMutableArray
),字典( NSDictionary
或NSMutableDictionary
,字符串( NSString
或NSMutableString
)和NSNumber
。
常見的機制是在您的類上創建- (NSDictionary *)serialize
方法,該方法將其所有值復制到字典中以傳遞到NSJSONSerialization中。 然后實現- (id)initFromSerialization:(NSDictionary *)serialization
以反- (id)initFromSerialization:(NSDictionary *)serialization
對象。
我知道這有點晚了,但也許該類可以簡化您的編碼:
它是將Custom NSObject轉換為JSON可讀對象(NSArray或NSDictionary)的類。 試試看。 它具有跳過屬性和將屬性類型從字符串更改為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"}
這是反匯編程序類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.