sreejith.virgo
sreejith.virgo

Reputation: 343

How to map AfNetworking response object (JSON) to custom class?

I'm getting a JSON as response for a POST method using AFNetworking. I need to map the JSON to a custom class and the create an object containing values from the JSON.

JSON String

{
branch =     {
    address = "";
    email = "";
    globalId = 174;
    id = 0;
    mobile = 9846147442;
    name = "Sathish Clininc Branch1";
    netMdId = 132;
    numberOfDevices = 0;
    organisationName = "<null>";
    passPhrase =         (
    );
    phone = 04872279166;
    status = active;
};
error = "<null>";
netmd =     {
    globalId = 132;
    headOfficeAddress = "";
    headOfficeEmail = "[email protected]";
    headOfficeMobile = "";
    headOfficeName = Koorkenchery;
    headOfficePhone = "";
    id = 0;
    name = "Sathish Clinic";
    ownerAddress = "";
    ownerEmail = "[email protected]";
    ownerFirstName = Sathish;
    ownerLastName = Chandran;
    ownerMobile = "";
    ownerPhone = "";
    password = "aW8oFWDMOJUrIV3l7R7hqQ==";
    status = active;
    userName = sathish;
    userType = owner;
};
primary = 1;
retrieveAppointments =     (
);
retrieveDoctorsList =     (
);
retrievePatients =     (
);
retrieveScheduleList =     (
);
success = 1;
user =     (
);    }

Custom Class

#import <Foundation/Foundation.h>
#import "ErrorDTO.h"
#import "NetMdBranchDTO.h"
#import "NetMdDTO.h"
@interface NetMdActivationResponseDTO : NSObject
@property (nonatomic, strong)ErrorDTO* error;
@property (nonatomic, assign) BOOL success;
@property (nonatomic, strong) NetMdBranchDTO* branch;
@property (nonatomic, strong) NetMdDTO* netmd;
@property (nonatomic, strong) NSArray* user;
@property (nonatomic, strong) NSArray* retrieveDoctorsList;
@property (nonatomic, strong) NSArray* retrievePatients;
@property (nonatomic, strong) NSArray* retrieveScheduleList;
@property (nonatomic, strong) NSArray* retrieveAppointments;
@property (nonatomic, assign) BOOL primary;

@end

Upvotes: 4

Views: 4338

Answers (3)

Maciej Oczko
Maciej Oczko

Reputation: 1225

You can boost your value object with Mantle. It would be easier to maintain all transformations.

Upvotes: 6

diederikh
diederikh

Reputation: 25281

If your JSON keys match your custom class's properties then it is straight forward:

  1. Deserialize your JSON data into an NSDictionary using the NSJSONSerialization class.
  2. Fill your custom objects properties using [object setValuesForKeysWithDictionary:deserializedDictionary]

If the keys do not match than set the properties like object.user = deserializedDictionary[@"userName"]

Another option is to allow custom object to accept all kinds of keys and do the mapping within the object by overriding -setValue:ForKey:

Upvotes: 5

Lithu T.V
Lithu T.V

Reputation: 20021

Use JSON Accelerator generate your classes add to code execute

If you want to deal it by yourself

{} : Represents object/Dictionary
[] : Represents array

Dive in write every drill down and get what you require

Upvotes: 1

Related Questions