Hello. I do a test task and a question arises.

The bottom line is this: I make a request to the server, get a JSON line in response, and then feed it to the JSONModel library .

In normal examples, everything is fine, but in one answer there is a field that returns false without values:

"names":false 

with one line:

 "names":"Vasiliy" 

and with a multiple array:

 "names":["Vasiliy", "Petr"] 

And I get an error as a result

Invalid JSON data. The JSON type mismatches the expected type ...

In the model, this field

 @property (nonatomic) NSArray <Optional> *names; 

What can be done with this?

  • to check what came before driving into a variable - Max Mikheyenko

1 answer 1

Judging by the examples attached to the JSONModel library, you can try to implement your own methods to establish the values ​​of the properties of the model. As far as I know, it’s impossible to combine all possible values ​​of the names field of various types in one property, so you have to create 3 separate properties that can be combined into a separate class. It may look like this:

 @interface Names : NSObject @property (nonatomic, retain) NSString* names_string; @property (nonatomic, retain) NSNumber* names_bool; @property (nonatomic, retain) NSArray* names_array; @end 

Suppose your model class looks like this:

 @interface MyModel : JSONModel @property (nonatomic) Names* names; @end 

Add an implementation of the following methods:

 @implementation Names @end @implementation MyModel - (void)setNamesWithNSString:(NSString *)string { self.names = [[Names alloc] init]; self.names.names_string = string; } - (void)setNamesWithNSArray:(NSArray *)array { self.names = [[Names alloc] init]; self.names.names_array = array; } - (void)setNamesWithNSNumber:(NSNumber *)boolValue { self.names = [[Names alloc] init]; self.names.names_bool = boolValue; } @end 

Now, after creating an object of your model, check which of the properties (names_bool, names_array or names_string) was initialized.