0

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.

Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
MCR
  • 1,633
  • 3
  • 21
  • 36
  • "but the dictionary parameter was not an 'NSDictionary'." What does that say to you??? – Hot Licks Mar 07 '14 at 20:22
  • Where is the exception stack trace? – Hot Licks Mar 07 '14 at 20:27
  • 1
    I think your CategoryResponse object should be coded `@property (strong, nonatomic) Categories *categories;` – Hot Licks Mar 07 '14 at 20:36
  • can you handle the response data to generate your dictionary: [NSJSONSerialization jsonObjectWithData:data..] – aug2uag Mar 07 '14 at 21:45
  • HotLicks - That was the solution. I think I was staring at my screen for far too long when I got hung up on this :) If you want to post an answer, I will accept it. – MCR Mar 10 '14 at 12:54

1 Answers1

-1

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.

aug2uag
  • 3,379
  • 3
  • 32
  • 53
  • initWithString handles the JSON decoding. – Hot Licks Mar 07 '14 at 21:53
  • rightly so, but why data -> string -> dictionary when you can data -> dictionary? thanks for the down vote btw, great to have you feeling good about yourself – aug2uag Mar 07 '14 at 21:58
  • Because the OP is using the JSONModel toolkit, and that's the way it defines things. It's a Jackson wannabe toolkit that creates fully clothed Objective-C objects from the JSON, vs exposing the dictionaries and arrays. (In other words, your answer was "not useful".) – Hot Licks Mar 07 '14 at 22:05
  • the point is to get the JSON to create models no? Whatever libraries are most likely utilizing NSURLConnection and NSJSONSerialization. I personally find your logic offensive and useless. – aug2uag Mar 07 '14 at 22:06
  • The point is to create a CategoryResponse object out of the JSON. – Hot Licks Mar 07 '14 at 22:07
  • (I will agree that it would be more efficient to run the data through NSJSONSerialization and feed the resulting dictionary into `initWithDictionary`, but that wouldn't address the OP's problem.) – Hot Licks Mar 07 '14 at 22:08
  • The CategoryResponse property categories is a dictionary from my understanding, and if it is not then there may be some issue with the initWithString method that is not included in the OP's request. – aug2uag Mar 07 '14 at 22:09
  • But `initWithString` creates the *entire* CategoryResponse object. The OP is having trouble with the `categories` component specifically (probably due to declaring that as a dictionary vs a Categories object), but that's fairly deep inside the `initWithString` logic. – Hot Licks Mar 07 '14 at 22:12