ios - 尝试使用 JSONModel : Invalid type in JSON write (. 序列化后出错 ..)

标签 ios objective-c json nsarray jsonmodel

我正在尝试使用 Framework JSOSNModel 序列化我的对象。但是我收到以下错误:

[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

预订.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

最佳答案

我刚遇到同样的问题,问题如下:

@property (nonatomic, retain) NSMutableArray * dienstetest;

这必须更改以指定您在数组中拥有的对象类型,因此像这样的东西将使它起作用(假设它是好对象,因为我的德语不是那么好)-您已经有了协议(protocol),所以您应该能够用这个指定类型:

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

关于ios - 尝试使用 JSONModel : Invalid type in JSON write (. 序列化后出错 ..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686550/

相关文章:

ios - 当应用程序权限被禁用时,SLComposeViewController 出现问题

ios - Segue 不在 ViewController 之间传递数据

ios - iOS7 bar标题过长时UINavigationBar后退按钮标题被隐藏

android - Android ListView 中的 JSONArray

c# - 在哪里可以找到适用于 C# 的简单但灵活的 JSON 解析器?

iOS:无法在沙盒环境中恢复消耗品购买

ios - 当我打开搜索栏时,导航栏隐藏了

ios - swift 在 View Controller 之间进行委托(delegate)

objective-c - 核心数据 : Store cannot hold instances of entity (Cocoa Error: 134020)

python - 值错误: No JSON object could be decoded