Question

Is it possible to parse json to an array of JSONModel objects using JSONModel? Like parse json below:

[{"id" : 1}, {"id" : 2}]

to two JSONModel objects having property of "id".

Was it helpful?

Solution

You should use arrayOfModelsFromDictionaries: on your model class like so:

NSArray* models = [YourModelClass arrayOfModelsFromDictionaries: objects];

This will go over "objects" and convert each dictionary to a model and you get the result in models. If any item in objects fail to convert to model "models" will be nil.

Here's the class docs for this method: http://jsonmodel.com/docs/Classes/JSONModel.html#//api/name/arrayOfModelsFromDictionaries:

OTHER TIPS

Why not trying BWJSONMatcher?

First you should declare your own data model:

@interface MyDataModel : NSObject
@property (nonatomic, assign) NSInteger id;
@end

Then you can easily get your array with BWJSONMatcher within one line:

NSArray *jsonArray = [BWJSONMatcher matchJSON:jsonString withClass:[MyDataModel class]];

Hope this could help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top