Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error after trying to serialize with JSONModel: Invalid type in JSON write (...)

i'am trying to serialize my object with the Framework JSOSNModel. But i am getting the following error:

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

Here is my Sourcecode:

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

you can see in the screenshot that the objects are there but i can't serialize it. please help. screenshot2

like image 598
Eray Geveci Avatar asked Dec 02 '25 11:12

Eray Geveci


1 Answers

I just had the same kind of problem, the problem is the following :

@property (nonatomic, retain) NSMutableArray * dienstetest;

This has to be change to specify the type of object you have in your Array so something like this will make it works (assuming it's the good object because my german isn't this good) - you already have the protocol so you should be able to specify the type with this:

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

like image 107
Clad Clad Avatar answered Dec 05 '25 00:12

Clad Clad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!