简体   繁体   English

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

[英]Why is my JSONModel Class being treated as a NSDictionary?

This is my JSON fetched from the API: 这是从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";
} 

And here is my model 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

Now, I am trying to get the values like this: 现在,我正在尝试获取如下值:

restaurant = [[Restaurant alloc] initWithDictionary

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

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

The values are coming fine till the VenueModel object, after that when I am trying to access the LocationModel object, ie 直到VenueModel对象的值都很好,之后,当我尝试访问LocationModel对象时,即

LocationModel *locationObj = obj.location

, its throwing the following error: ,则抛出以下错误:

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

Somebody please help me out, I need the location object. 有人请帮帮我,我需要定位对象。

NSJSONSerialization will produce NSDictionaries, NSArrays, NSStrings, NSNumbers and a few other basic types from a JSON string. NSJSONSerialization将根据JSON字符串生成NSDictionaries,NSArrays,NSStrings,NSNumbers和其他一些基本类型。

Your idea to initialize a custom object ( [[Restaurant alloc] initWithDictionary... ) is the right idea, but that idea must be applied to nested structures as well. 初始化自定义对象( [[Restaurant alloc] initWithDictionary... )的想法是正确的想法,但是该想法也必须应用于嵌套结构。 So, for example, the Restaurant initializer must apply that idea to its venues array. 因此,例如, Restaurant初始化程序必须将该想法应用于其venues数组。

Otherwise, you'll get the crash you're getting, treating one of the venue objects as if its a VenueModel instead of what it really is (an NSDictionary). 否则,您将发生崩溃,将一个场所对象视为其VenueModel而不是其实际值(NSDictionary)。

You didn't post initWithDictionary , but it needs to look something like this, especially the part where the objects it contains are initialized... 您没有发布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;
}

Moreover, as you can see, VenueModel must also follow this practice, turning the location data into a LocationModel , and so on... 此外,如您所见, VenueModel还必须遵循此做法,将位置数据转换为LocationModel ,依此类推...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM