Search code examples
iosobjective-ccocoajsonmodel

JsonModelLib: Unable to parse if a variable does not exists in JSON


I know my question is a little confusing so please allow me to elaborate.

JSON Parsing Lib : JSONModelLib

MODEL

#import "JsonModelLib/JSONModel.h"


@interface CustomerDetailsModel : JSONModel


@property (nonatomic, strong) NSString *description;

@property (nonatomic, strong) NSString *firstName;

@property (nonatomic, strong) NSString *lastName;

@property (nonatomic, strong) NSString *activatedTill;

@property (nonatomic, strong) NSString *birthdate;

@property (nonatomic, strong) NSString *phone;

@property (nonatomic, strong) NSString *id;

@property (nonatomic, strong) NSString *statusCode;

@end

JSON string

NSString* str = @"\
{\
\"description\": \"None\",\
\"firstName\": \"vikas\",\
\"lastName\": \"bansal\",\
\"activatedTill\": \"2016-01-17 09:04:11\",\
\"email\": \"bansal\",\
\"birthdate\": \"None\",\
\"phone\": \"None\",\
\"id\": \"1053\",\
\"statusCode\": \"1600\"\
}\
";

Parsing

CustomerDetailsModel* c = [[CustomerDetailsModel alloc] initWithString:str error:nil];

PROBLEM

The problem is that some time when json does not contain a variable that exists in MODEL Why?? becuase the REST API I am using does not render variable into JSON if there is no value in it.

For an example suppose there is no value in birthdate then it will not be included in JSON so when I will try to parse the JSON what I will get is an error.

Please help or please suggest what should I do

Note: I have already gone far ahead with JsonModelLib so if you asking me to use something else or some other lib then I will be a great pain for me. Please please please try to suggest something without changing the lib if it is possible

Many many thanks...


Solution

  • Use an optional model property - there's an example in the repo's README. https://github.com/icanzilb/JSONModel#optional-properties-ie-can-be-missing-or-null