1

I am using jsonmodel for mapping my response after getting from my service. Now the problems my service is giving me the response as an array of dictionaries but I can't map the array as it is not assigned against any key.

enter image description here

I have a JsonModel named ContactsModel.h which hold all the parameters identifier which mapped perfectly, but then I need to take the array of this model. So, I created another JsonModel, ContactListModel to hold that.

#import <JSONModel/JSONModel.h>
#import "ContactsModel.h"

@interface ContactListModel : JSONModel

@property(nonatomic, assign) NSArray<ContactsModel> *contacts;

@end

But see, my array is not assigned against any key. So, I think, that is why it is not being able to map.

Cane anyone please help me to deal with this situation.

Natasha
  • 6,651
  • 3
  • 36
  • 58
  • I see you are getting an array of dictionaries in response, what issue you are facing? – Bharat Jul 02 '16 at 11:10
  • any errors? just no data? – Joshua Jul 03 '16 at 02:05
  • The response object is an array of dictionaries which is cool but the fact is that array is not assigned against any key. So, I can't do key-value pairing. – Natasha Jul 03 '16 at 15:57
  • Can u add the declaration of `ContactsModel` I think there must be something wrong inside. Or there should be an error given by JSONModel when you create the `contract` using the `NSString` – Clad Clad Jul 13 '16 at 14:48

2 Answers2

0

Hope this helps:

NSMutableArray *objects = [NSMutableArray new];

for(NSDictionary *dictionary in responseObject){
[objects addObject:dictionary];
//do your modeling
}
Hussein
  • 407
  • 5
  • 16
0

@natasha

I do think you can use JSONModel's method arrayOfModelsFromDictionaries:

So in your request you can do something like this:

 NetworkRequest:<whatEverRequest> params:<whatEverParams> completion:(id response){

if([response isKindOfClass:[NSArray class]]){

  ContactListModel *list = [ContactListModel new];
  list.contacts = [ContactsModel arrayOfModelsFromDictionaries: response];
   // I just followed the jsonModel API. maybe latest version has initWithArray? 
}

} error:(NSError *error){}];

I hope this solves your problem

Joshua
  • 2,432
  • 1
  • 20
  • 29