0

I am not able to initialise a model from JSONModel. I am using JSONModel (as after this poc the .json is huge and needs a third party library like this to take care.). My steps are as follows:

  1. Pod installed the JSONModel
  2. Create a simple model that you want the .json got from the url to initialise
    your model in your project.
  3. As per the description I am getting the nil value out of it.

    Analysing why I got the nil value, I think one might be the reason.

    if (!json || ![json isKindOfClass:[NSArray class]]) 
      return nil; 
    

here I am getting .json value, so checking this

[json isKindOfClass:[NSArray class]], 

I am getting a nil value .

In .h file of my model. I create a property of jokeid, text and in .m file I am using this code

+(JSONKeyMapper*)keyMapper{
    return [[JSONKeyMapper alloc]initWithModelToJSONDictionary:@{@"jokeid":@"jokeid", @"text":@"text"}];

}

I need some guidance in this "arrayOfModelsFromData:" of JSONModel . I am posting my snapshot and the code

code is in github https://github.com/utsav475908/sdfs Debugging snapshot is here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kumar Utsav
  • 2,761
  • 4
  • 24
  • 38
  • Can you show the entire "jsonDict" and structure you have for JokeModel. Reason being is that jsonModel matches the data to your property and its datatype. If it cannot be match it will return you with an empty object. – Joshua Dec 16 '16 at 06:11
  • Yes, I have given the link of the github. To answer your question , jsonDict is like this : { jokeid = 7; text = "There used to be a street named after Chuck Norris but it was changed because nobody crosses Chuck Norris and lives"; } – Kumar Utsav Dec 16 '16 at 06:13
  • The github goes to Jsonmodel library page. Not ur code. zzz – GeneCode Dec 16 '16 at 06:14
  • github https://github.com/utsav475908/sdfs is the link to my code – Kumar Utsav Dec 16 '16 at 06:15
  • #GeneCode can you help me , why I am getting nil value. – Kumar Utsav Dec 16 '16 at 06:25

1 Answers1

0

You are getting a nil value because your JSON object contains a dictionary, not an array.

For correct work try to change your json payload to something like this:

[
     {
     "jokeid": 1,
     "text": "There used to be a street named after Chuck Norris but it was changed because nobody crosses Chuck Norris and lives"
     },
     {
     "jokeid": 2,
     "text": "There used to be a street named after Chuck Norris but it was changed because nobody crosses Chuck Norris and lives"
     },
     {
     "jokeid": 3,
     "text": "There used to be a street named after Chuck Norris but it was changed because nobody crosses Chuck Norris and lives"
     },
     {
     "jokeid": 4,
     "text": "There used to be a street named after Chuck Norris but it was changed because nobody crosses Chuck Norris and lives"
     },
]

and you will receive an array with 4 objects. Screenshot