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.
我正在使用AFNetworking获取JSON作为POST方法的响应。我需要将JSON映射到自定义类,并创建一个包含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
3 个解决方案
#1
6
You can boost your value object with Mantle. It would be easier to maintain all transformations.
您可以使用Mantle提升您的价值对象。维护所有转换会更容易。
#2
5
If your JSON keys match your custom class's properties then it is straight forward:
如果您的JSON键与您的自定义类的属性匹配,那么它是直截了当的:
- Deserialize your JSON data into an NSDictionary using the
NSJSONSerialization
class. - Fill your custom objects properties using
[object setValuesForKeysWithDictionary:deserializedDictionary]
使用NSJSONSerialization类将JSON数据反序列化为NSDictionary。
使用[object setValuesForKeysWithDictionary:deserializedDictionary]填充自定义对象属性
If the keys do not match than set the properties like object.user = deserializedDictionary[@"userName"]
如果键不匹配,则设置像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:
另一个选择是允许自定义对象接受所有类型的键,并通过重写-setValue来执行对象内的映射:ForKey:
#3
1
Use JSON Accelerator generate your classes add to code execute
使用JSON Accelerator生成您的类添加到代码执行
If you want to deal it by yourself
如果你想自己处理它
{} : Represents object/Dictionary
[] : Represents array
Dive in write every drill down and get what you require
潜水写下每次下钻并获得你需要的东西
#1
6
You can boost your value object with Mantle. It would be easier to maintain all transformations.
您可以使用Mantle提升您的价值对象。维护所有转换会更容易。
#2
5
If your JSON keys match your custom class's properties then it is straight forward:
如果您的JSON键与您的自定义类的属性匹配,那么它是直截了当的:
- Deserialize your JSON data into an NSDictionary using the
NSJSONSerialization
class. - Fill your custom objects properties using
[object setValuesForKeysWithDictionary:deserializedDictionary]
使用NSJSONSerialization类将JSON数据反序列化为NSDictionary。
使用[object setValuesForKeysWithDictionary:deserializedDictionary]填充自定义对象属性
If the keys do not match than set the properties like object.user = deserializedDictionary[@"userName"]
如果键不匹配,则设置像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:
另一个选择是允许自定义对象接受所有类型的键,并通过重写-setValue来执行对象内的映射:ForKey:
#3
1
Use JSON Accelerator generate your classes add to code execute
使用JSON Accelerator生成您的类添加到代码执行
If you want to deal it by yourself
如果你想自己处理它
{} : Represents object/Dictionary
[] : Represents array
Dive in write every drill down and get what you require
潜水写下每次下钻并获得你需要的东西