2013-10-18 89 views
1

我試圖序列,其中有項目的NSMutableArray我的車對象,但得到的:序列化自定義對象到JSON其中包含的NSMutableArray

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (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 

我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線。我錯過了什麼?

+1

邊注:不使用@synthesize,這不再需要。 –

回答

4

這行:[itemsToSerialize addObject:item];應該[itemsToSerialize addObject:itemDict];。其結果是,你想序列化項目本身的陣列,它給你所看到的例外。

+0

咄...謝謝! – PkL728

3

NSJSONSerialization只能在陣列(NSArrayNSMutableArray),字典(NSDictionaryNSMutableDictionary,字符串(NSStringNSMutableString),和NSNumber

的共同機制是在你的類中創建一個- (NSDictionary *)serialize方法,所有副本它的值轉換成字典傳遞到NSJSONSerialization然後實現- (id)initFromSerialization:(NSDictionary *)serialization反序列化對象

+0

謝謝你的小費! – PkL728

1

我知道這是有點晚了,但也許這個類可以緩解你的編碼:。

及其該變換定製NSObject的至JSON可讀對象(的NSArray或NSDictionary的)的一類。試試看。它跳過從字符串屬性和更改的屬性類型NSInteger的或浮動的能力,它也可以改變最終的輸出屬性名稱的可以說

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"} 

這裏是類Disassembler

相關問題