Nested Dictionary in JSONModel

I am getting an error when creating my JSONModels.

Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'." UserInfo=0x9bc2340 {kJSONModelKeyPath=categories.Data, NSLocalizedDescription=Invalid JSON data: Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'.}

Here is the response I am getting...

{
    "categories": {
        "Data": [
            {
                "Id": 19,
                "Name": "",
                "Description": "",
                "ImageURL": "",
                "FullSizeImageUrl": "",
                "ParentCategoryId": 0
            }
        ],
        "Total": 1
    }
}

Here are my Models

@interface CategoryResponse : JSONModel
@property (strong, nonatomic) NSDictionary <Categories> *categories;
@end

@protocol Categories @end
@interface Categories : JSONModel
@property (assign, nonatomic) int Total;
@property (strong, nonatomic) NSArray<CategoryData> *Data;
@end

@protocol CategoryData @end
@interface CategoryData : JSONModel
@property (strong, nonatomic) NSString <Optional> *Name;
@property (strong, nonatomic) NSString <Optional> *Description;
@property (strong, nonatomic) NSString <Optional> *ImageURL;
@property (strong, nonatomic) NSString <Optional> *FullSizeImageUrl;
@property (assign, nonatomic) int Id;
@property (assign, nonatomic) int ParentCategoryId;

void (^success)(AFHTTPRequestOperation *, id) = ^(AFHTTPRequestOperation *op, id data) {
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSError *error;

    categories = [[CategoryResponse alloc] initWithString:responseString error:&error];

    complete(nil, categories);
};

I realize @property (strong, nonatomic) NSDictionary <Categories> *categories; is not that proper way to do this, but I don't know what is.

Answers


I would like to think you can handle the assignment of your dictionary as such:

void (^success)(AFHTTPRequestOperation *, id) = ^(AFHTTPRequestOperation *op, id data) {
    categories = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    complete(nil, categories);
};

Though I am not very familiar with AFNetworking as I like to use NSURLConnection.


Need Your Help

Checking for valid OTP using session variable

php forms session session-variables

I am trying to send an SMS OTP and retrieve it from user to verify the user.

JQuery/JavaScript - Creating a table from list of words

javascript jquery

At the moment this creates the table for the list of words, but only in one column. How do I get it to print into a 4x5 table?