Search code examples
iosobjective-cjsonparsinggithub-mantle

Mantle parsing array


I am using mantle framework to parse JSON file. My JSON object Looks like this

[{
   key:value
   key:value
 },
 {
   key:value
   key:value
 } ]

My object is array that doesn't have key. How could we parse this array? How should the JSONKeyPathsByPropertyKey method be implemented?

As mentiond on the Library description

This method Specifies how to map property keys to different key paths in JSON

+ (NSDictionary *)JSONKeyPathsByPropertyKey {

      return @{
        @"items" : @"",
      };

So how could we map array property to JSON object that doesn't have a key ?


Solution

  • I assume, You are getting an array in response of an API call and you want to parse it.

    So as per your JSON. You need to create a MTLModel subclass for the type of objects in the array.

    Then you can parse the array and create the models of the object types in array. Like this:

    NSArray *objects = [MTLJSONAdapter modelsOfClass:[model class] fromJSONArray:[PASS THE ARRAY] error:nil];
    

    Hope that helps.