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

I get JSON as an answer for the POST method using AFNetworking. I need to map JSON to a custom class and create an object containing values ​​from 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 = "sreejith@gmail.com";
    headOfficeMobile = "";
    headOfficeName = Koorkenchery;
    headOfficePhone = "";
    id = 0;
    name = "Sathish Clinic";
    ownerAddress = "";
    ownerEmail = "sreejith@gmail.com";
    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
+4
source share
3 answers

You can increase your value object with a mantle . It would be easier to support all the transformations.

+6
source

If your JSON keys match your custom class properties, this is straightforward:

  • JSON NSDictionary NSJSONSerialization.
  • [object setValuesForKeysWithDictionary:deserializedDictionary]

, object.user = deserializedDictionary[@"userName"]

- , -setValue:ForKey:

+5

Use JSON Accelerator to generate your classes, adding to code execution

If you want to deal with it yourself

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

Immerse yourself in every entry and get what you need

+1
source

Source: https://habr.com/ru/post/1525759/


All Articles