2013-10-30 26 views
3

我試圖用框架JSOSNModel序列化我的對象。但我發現了以下錯誤:嘗試使用JSONModel序列化後出錯:在JSON中寫入無效類型(...)

[JSONModel.m:915] EXCEPTION: Invalid type in JSON write (DienstleistungModel)

這裏是我的源代碼:

buchung.m

-(NSMutableArray *) FetchDienstleistungenImWarenkorb 
{ 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSManagedObjectContext *context = [app managedObjectContext]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Warenkorb" inManagedObjectContext:context]; 

    [request setEntity:entity]; 

    NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"vondatum" ascending:YES]; 
    NSArray *sortArray = [NSArray arrayWithObjects:sort1, nil]; 

    [request setSortDescriptors:sortArray]; 

    NSError *error; 
    NSArray *fetchedObjects = [[app managedObjectContext]executeFetchRequest:request error:&error]; 

    if (fetchedObjects == nil) { 
     NSLog(@"Houston, we have a problem: %@", error); 
    } 

    DienstleistungModel *dm = [[DienstleistungModel alloc]init]; 

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

    for (NSArray *event in fetchedObjects) { 
     dm.dienstleistungid = [event valueForKey:@"produktid"]; 
     dm.vondatum = [event valueForKey:@"vondatum"]; 
     dm.bisdatum = [event valueForKey:@"bisdatum"]; 
     dm.menge = [event valueForKey:@"menge"]; 
     [produkt addObject:dm]; 
     } 
      return product;   // Here iam getting a list of products, saved in a mutable array 

} 


(IBAction)nextPressed:(id)sender { 

    Booking *aktuellebuchung = [[Booking alloc]init]; 
    aktuellebuchung.bestuhlungsid = @"3"; 
    aktuellebuchung.vondatum = self.vonDatumLabel.text; 
    aktuellebuchung.bisdatum = self.bisDatumLabel.text; 
    aktuellebuchung.thema = self.themaTextView.text; 
    aktuellebuchung.personenanzahl = self.anzahlPersonenLabel.text; 
    aktuellebuchung.veranstalter = self.veranstalterLabel.text; 
    aktuellebuchung.dienstetest = [self FetchDienstleistungenImWarenkorb]; 

    NSString *test = [aktuellebuchung toJSONString]; // Here is the error 

booking.h

@interface Booking : JSONModel 
@property (nonatomic, retain) NSString * bestuhlungsid; 
@property (nonatomic, retain) NSString * bisdatum; 
@property (nonatomic, retain) NSString * vondatum; 
@property (nonatomic, retain) NSString * personenanzahl; 
@property (nonatomic, retain) NSString * thema; 
@property (nonatomic, retain) NSString * veranstalter; 
@property (nonatomic, retain) NSMutableArray * dienstetest; 

@end 

DienstleistungModul.h

@protocol DienstleistungModel @end 
@interface DienstleistungModel : JSONModel 
@property (nonatomic, retain) NSNumber * dienstleistungid; 
@property (nonatomic, retain) NSString * bisdatum; 
@property (nonatomic, retain) NSString * vondatum; 
@property (nonatomic, retain) NSNumber * menge; 
@end 

screenshot

你可以在截圖的對象是沒有看到,但我不能把它序列化。請幫忙。 screenshot2

+0

你對數組屬性有DienstleistungModel協議嗎?當你在你的問題 –

+0

中粘貼代碼只是一個隨機的想法時,bcz可能小於或大於符號丟失 - 嘗試將dienstetest聲明爲NSArray,看看是否會走低谷 –

回答

2

我有同樣的問題,這個問題是這樣的:

@property (nonatomic, retain) NSMutableArray * dienstetest;

這必須改變,以指定你在你的陣列所以像這種對象的類型將它的工作原理(假設它是個好對象,因爲我的德國是不是這個好) - 你已經有協議,所以你應該能夠說明這種類型:

@property (nonatomic, retain) NSArray<DienstleistungModel> * dienstetest;

相關問題