3

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

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
Eray Geveci
  • 1,099
  • 4
  • 17
  • 39
  • do you have the DienstleistungModel protocol on the array property? bcz probably the less than and greater than symbols got lost when you pasted the code in your question – Marin Todorov Oct 31 '13 at 12:00
  • just a random idea - try declaring dienstetest as an NSArray and see if that's gonna go trough – Marin Todorov Oct 31 '13 at 12:01

1 Answers1

2

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;

Clad Clad
  • 2,653
  • 1
  • 20
  • 32