2

I'm trying to fetch some data from a API call and map those objects to a model object. In my case I have an object inside an object. This is working and received JSON. And it's mapping fine with the very first object. In my JSON I have an array and another array inside it. So I did the following to get this done.

Object mapping

+(NSArray *)fromJSON:(NSDictionary *)objectNotation error:(NSError *__autoreleasing *)error{
    NSError *localError = nil;

    NSDictionary *parsedObject = [[NSDictionary alloc] initWithDictionary:objectNotation];

    if (localError != nil) {
        *error = localError;
        return nil;
    }
    NSMutableArray *posts = [[NSMutableArray alloc] init];

    NSArray *results = [parsedObject valueForKey:@"data"];
    NSLog(@"Count %lu", (unsigned long)results.count);

    for (NSDictionary *groupDic in results) {
        Post *post = [[Post alloc] init];
        post.postBody = [NSMutableArray array];

        for (NSString *key in groupDic) {
            if ([post respondsToSelector:NSSelectorFromString(key)]) {
                [post setValue:[groupDic valueForKey:key] forKey:key];
            }
        }
        [posts addObject:post];
    }
    return posts;
}

This one makes multiple metadata than I expected because of the for loop.

Model classes

@interface OCPost : NSObject
@property(nonatomic) NSInteger postId;
@property(nonatomic) NSInteger userId;
@property(nonatomic) NSInteger points;
@property(strong, nonatomic) NSMutableArray *body;
@end

I want to map the body array with array of Body objects. In my body class

@interface Body : NSObject
@property (strong, nonatomic) NSString *value;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSMutableArray *metaData;
@end

And also the metadata should be map with the following object.

@interface MetaData : NSObject
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *metaDataDescription;
@end

How may I make an object like,

Post
  |_Body1
  |  |_MetaData1
  |  |_MetaData2
  |  |_MetaData3
  |_Body3
  |  |_MetaData1
  |  |_MetaData2

Please help.

JSON

{
   "message": "Post ",
   "data": [
      {
        "postId": 1,
        "timestamp": "2016-06-14 22:37:02",
        "body": [
          {
            "textId": 1,
            "type": "link",
            "deleting": false,
            "metaData": [
          {
            "metadataId": 1,
            "type": "text",
            "value": "under "
          }
          ]
        },
        {
           "textId": 2,
           "type": "department",
           "deleting": false,
           "metaData": [
              {
                "metadataId": 2,
                "type": "text",
                "value": "under "
              }
           ]
        }
      ]
    }
  ]
}
codebot
  • 2,540
  • 3
  • 38
  • 89
  • have you checked up [mantle](https://github.com/Mantle/Mantle)? – Bartu Jun 15 '16 at 22:58
  • yes. i tried. But didn't solve my problem. Then I switched to this one.. – codebot Jun 15 '16 at 23:05
  • You also checked out [this](http://stackoverflow.com/questions/13883693/how-to-specify-child-objects-type-in-an-nsarray-with-mantle) feature? – Bartu Jun 15 '16 at 23:18
  • Could you please show a sample JSON data. – dichen Jun 15 '16 at 23:56
  • You could try the JSONModel library, it allows for what you are trying to do. [link](https://github.com/icanzilb/JSONModel/blob/master/README.md#magical-data-modelling-framework-for-json) – Dennis W. Jun 16 '16 at 04:05
  • @dichen- i added the json – codebot Jun 16 '16 at 10:34
  • Nothing complex, value for key data is an array of bodies, each body has a value for key metadata which is array too. You should iterate over each array and create body object and metadata object. – Evgeny Karkan Jun 18 '16 at 06:35

1 Answers1

1

Ok, you can parse JSON and map it to your data models manually.
Here is how you can do it:

+(NSArray *)fromJSON:(NSDictionary *)objectNotation error:(NSError *__autoreleasing *)error{
    NSError *localError = nil;

    NSDictionary *parsedObject = [[NSDictionary alloc] initWithDictionary: objectNotation];

    if (localError != nil) {
        *error = localError;
        return nil;
    }
    NSMutableArray *posts = [[NSMutableArray alloc] init];

    NSArray *results = [parsedObject valueForKey:@"data"];
    NSLog(@"Count %lu", (unsigned long)results.count);

    for (NSDictionary *groupDic in results) {
        OCPost *post = [[OCPost alloc] init];
        post.body    = [NSMutableArray array];

        //1. Parse bodies
        NSArray *bodies = groupDic[@"body"];

        for (NSDictionary *aBody in bodies) {
            Body *body    = [[Body alloc] init];
            body.value    = aBody[@"textId"];
            body.name     = aBody[@"type"];
            body.metaData = [@[] mutableCopy];

            //2. Parse metadatas
            NSArray *metadatasOfBody = aBody[@"metaData"];

            for (NSDictionary *aMetadata in metadatasOfBody) {
                MetaData *metadata           = [[MetaData alloc] init];
                metadata.title               = aMetadata[@"type"];
                metadata.metaDataDescription = aMetadata[@"value"];

                //3. Add metadata to body
                [body.metaData addObject:metadata];
            }

            //4. Add body to post
            [post.body addObject: body];
        }

        //Logs to check post body and body metadata
        NSLog(@"post %@", post);
        NSLog(@"post body %@", post.body);

        for (Body *body in post.body) {
            NSLog(@"post body metadata %@", body.metaData);
        }

        //5. Add post to posts
        [posts addObject: post];
    }

    //Log to check return posts
    NSLog(@"posts %@", posts);

    return posts;
}

To test it you can represent your JSON as NSDictionary, like this:

- (NSDictionary*) aJSON
{
    return @{
        @"message": @"Post ",
        @"data": @[
                 @{
                 @"postId": @1,
                 @"timestamp": @"2016-06-14 22:37:02",
                 @"body": @[
                          @{
                          @"textId": @1,
                          @"type": @"link",
                          @"deleting": @false,
                          @"metaData": @[
                                       @{
                                       @"metadataId": @1,
                                       @"type": @"text",
                                       @"value": @"under "
                                       }
                                       ]
                          },
                          @{
                          @"textId": @2,
                          @"type": @"department",
                          @"deleting": @false,
                          @"metaData": @[
                                       @{
                                       @"metadataId": @2,
                                       @"type": @"text",
                                       @"value": @"under "
                                       }
                                       ]
                          }
                          ]
                 }
                 ]
        };
}

Replace objectNotation with [self aJSON] to be able test mapping without calling the server API, or of course, call server API endpoint, and try to test with response JSON.

Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38