为什么我的 JSONModel Class 被视为 NSDictionary?

Why is my JSONModel Class being treated as a NSDictionary?

这是我从 API:

中获取的 JSON
 {
categories =     (
            {
        icon =             {
            prefix =     "https://ss3.4sqi.net/img/categories_v2/food/italian_";
            suffix = ".png";
        };
        id = 4bf58dd8d48988d110941735;
        name = "Italian Restaurant";
        pluralName = "Italian Restaurants";
        primary = 1;
        shortName = Italian;
    }
);
hasPerk = 0;
id = 4b5bed2ef964a520971d29e3;
location =     {
    address = "HS-5";
    cc = IN;
    city = "New Delhi";
    country = India;
    crossStreet = "Kailash Colony Market";
    distance = 4061;
    formattedAddress =         (
        "HS-5 (Kailash Colony Market)",
        "New Delhi 110024",
        Delhi,
        India
    );
    labeledLatLngs =         (
                    {
            label = display;
            lat = "28.55272788981442";
            lng = "77.24192322690806";
        }
    );
    lat = "28.55272788981442";
    lng = "77.24192322690806";
    postalCode = 110024;
    state = Delhi;
};
name = "The Big Chill Cafe";
referralId = "v-1546605733";
} 

这是我的模型 class:

@interface LocationModel : JSONModel

@property (nonatomic) NSInteger distance;
@property (nonatomic) NSInteger postalCode;
@property (nonatomic) NSString *address;
@property (nonatomic) NSString *cc;
@property (nonatomic) NSString *city;
@property (nonatomic) NSString *country;
@property (nonatomic) NSString *crossStreet;
@property (nonatomic) NSString *lat;
@property (nonatomic) NSString *lng;
@property (nonatomic) NSString *state;
@property (nonatomic) NSArray *formattedAddress;
@property (nonatomic) NSArray *labeledLatLngs;
@end


@protocol VenueModel;

@interface VenueModel : JSONModel

@property (nonatomic) NSArray *categories;
@property (nonatomic) BOOL hasPerk;
@property (nonatomic) NSString *id;
@property (nonatomic) LocationModel *location;
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *referralId;
@end

@interface Restaurant : JSONModel
@property (nonatomic) NSInteger confident;
@property (nonatomic) NSArray <VenueModel*> *venues;
@end

现在,我正在尝试获取这样的值:

restaurant = [[Restaurant alloc] initWithDictionary

 [NSJSONSerialization JSONObjectWithData:JSON            
options:NSJSONReadingAllowFragments error:nil][@"response"] error:&error];

VenueModel *obj = [restaurant.venues firstObject];
LocationModel *locationObj = obj.location;

值在 VenueModel 对象之前都很好,之后当我尝试访问 LocationModel 对象时,即

LocationModel *locationObj = obj.location

,它抛出以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI location]: unrecognized selector sent to instance 0x600001b82880

有人请帮帮我,我需要位置对象。

NSJSON序列化将从 JSON 字符串生成 NSDictionaries、NSArrays、NSStrings、NSNumbers 和一些其他基本类型。

您初始化自定义对象的想法 ([[Restaurant alloc] initWithDictionary...) 是正确的想法,但该想法也必须应用于嵌套结构。因此,例如,Restaurant 初始化程序必须将该想法应用于其 venues 数组。

否则,您将遇到崩溃,将其中一个场地对象视为 VenueModel 而不是它的真实面目(NSDictionary)。

你没有 post initWithDictionary,但它需要看起来像这样,尤其是 它包含 的对象被初始化的部分。 ..

- (id)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    // ...
    self.venues = [NSMutableArray array];
    if (self) {
        for (NSDictionary *venueDictionary in dictionary[@"venues"]) {
            VenueModel *venue = [[VenueModel alloc] initWithDictionary:venueDictionary];
            [self.venues addObject:venue];
        }
    }
    // and so on, for every other object type nested in dictionary
    return self;
}

而且,如你所见,VenueModel也必须遵循这种做法,将位置数据变成LocationModel,等等...