0

How can I create a ConvertOnDemand 'NSArray *' using the JSONModel library from the following JSON response: -

[
 {"id": 1, "name": "jim"},
 {"id": 2, "name": "lovy"}
]

Please check here if you want to know more JSONModel ConvertOnDemand (https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=jsonmodel+convertonDemand).

aprofromindia
  • 1,111
  • 1
  • 13
  • 27

3 Answers3

0

If you're open to using the native Foundation framework instead, you could do something like this

NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err];

If your JSON is in the form of a string, just convert to NSData before calling the above

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

This solution will give you an NSArray of NSDictionaries

Chris
  • 7,270
  • 19
  • 66
  • 110
  • thanks, but what I need is an NSArray that Converts on Demand. Please check here for details (https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=jsonmodel+convertonDemand) – aprofromindia Aug 10 '15 at 17:03
0

May be BWJSONMatcher is what you are looking for.

Declare your data model that matches your json string:

@interface YourDataModel : NSObject
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString *name;
@end

Then your can use BWJSONMatcher to convert the json string to an NSArray which can be directly used in your ViewControllers.

NSArray *jsonArray = [BWJSONMatcher matchJSON:jsonString withClass:[YourDataModel class]];
Burrows Wang
  • 249
  • 1
  • 7
0

ConvertOnDemand is optional for your model class, if your entity class is Person class, declare the array property like this,

@property (nonatomic, strong) NSArray<Person, ConvertOnDemand> *persons;

@property (nonatomic, strong) NSArray<Person> *persons;

For the former one, the persons array type is JSONModelArray[Person], it just means all the person object has conformed Person protocol. For the later one, the type is NSArray with unconfirmed objects, but they are really Person type.

Itachi
  • 5,777
  • 2
  • 37
  • 69