我使用AFNetworking
获取 JSON 作为 POST 方法的响应。
我需要将 JSON 映射到自定义类,并创建一个包含 JSON 值的对象。
JSON字符串
{
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 = (
); }
自定义类
#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
答案 0 :(得分:6)
您可以使用Mantle来提升您的价值对象。维护所有转换会更容易。
答案 1 :(得分:5)
如果您的JSON键与您的自定义类的属性匹配,那么它就是直接的:
NSJSONSerialization
类将您的JSON数据反序列化为NSDictionary。[object setValuesForKeysWithDictionary:deserializedDictionary]
如果键不匹配,请设置object.user = deserializedDictionary[@"userName"]
另一种选择是允许自定义对象接受所有类型的键并通过覆盖-setValue:ForKey:
答案 2 :(得分:1)
使用JSON Accelerator生成类添加到代码执行
如果您想自己处理
{} : Represents object/Dictionary
[] : Represents array
潜入每次下钻并获得所需内容