Ios MOTIS对象映射,使用NSDictionary和NSArray值如何指定数组元素的类型?
我有json {“类型”:{ “食品”:[{“cve”:“1”,“描述”:“比萨饼”},{“cve”:“2”,“描述”:“餐厅”},{“cve”:“3”,“描述”:“咖啡馆”}], “健康”:[{“cve”:“3”,“说明”:“药房”},{“cve”:“4”,“说明”:“医院”}] }} 类型.hIos MOTIS对象映射,使用NSDictionary和NSArray值如何指定数组元素的类型?,ios,objective-c,json,Ios,Objective C,Json,我有json {“类型”:{ “食品”:[{“cve”:“1”,“描述”:“比萨饼”},{“cve”:“2”,“描述”:“餐厅”},{“cve”:“3”,“描述”:“咖啡馆”}], “健康”:[{“cve”:“3”,“说明”:“药房”},{“cve”:“4”,“说明”:“医院”}] }} 类型.h #import <Foundation/Foundation.h> @interface Types: NSObject @property (nonatomic, copy) NSDi
#import <Foundation/Foundation.h>
@interface Types: NSObject
@property (nonatomic, copy) NSDictionary *types;
@end
我用NSDictionary的NSArray获得NSDictionary
但我需要NSDictionary和NSArray的子类型
我试着用
+ (NSDictionary*)mts_arrayClassMapping
{
return @{mts_key(types): Subtype.class};
}
但是没有成功
如何使用Motis获取这些信息据我所知,您的
类型对象没有正确定义。如果您的属性类型为NSDictionary*
,并且接收到的JSON是一个字典,Motis将不会执行任何自动转换,因为类型已经匹配(您正在接收一个字典,并且您的属性类型为NSDictionary)
因此,必须按照JSON结构实现类型
对象。这意味着您的类型
对象必须具有两个类型数组属性,一个用于食品
,另一个用于健康
。然后,使用方法+mts\u arrayClassMapping
可以将数组的内容类型指定为子类型
以下是实施情况:
// ***** Type.h file ***** //
@interface Type: NSObject
@property (nonatomic, strong) NSArray *food;
@property (nonatomic, strong) NSArray *health;
@end
// ***** Type.m file ***** //
@implementation Type
+ (NSDictionary*)mts_mapping
{
return @{@"food": mts_key(food),
@"Health": mts_key(health),
};
}
+ (NSDictionary*)mts_arrayClassMapping
{
return @{mts_key(food): Subtype.class,
mts_key(health): Subtype.class,
};
}
@end
关于子类型的实现
,您的已经是正确的。但是,您不应使用属性名称说明
,因为它已被NSObject
使用:
// ***** Subtype.h file ***** //
@interface Subtype: NSObject
@property (nonatomic, assign) NSInteger cve;
@property (nonatomic, copy) NSString *theDescription;
@end
// ***** Subtypes.m file ***** //
@implementation Subtype
+ (NSDictionary*)mts_mapping
{
return @{@"cve": mts_key(cve),
@"description": mts_key(theDescription),
};
}
@end
最后,如上所述,您可以映射JSON,但首先必须提取键类型的“字典”,然后将其映射到“类型”模型对象
// Get the json data
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
// Extract the JSON dictionary of types.
NSDictionary *jsonType = [jsonObject objectForKey:@"Types"];
// Create a Type object
Type *type = [[Type alloc] init];
// Map JSON contents to the type object with Motis
[type mts_setValuesForKeysWithDictionary:jsonType];
希望这能解决你的问题
+ (NSDictionary*)mts_arrayClassMapping
{
return @{mts_key(types): Subtype.class};
}
// ***** Type.h file ***** //
@interface Type: NSObject
@property (nonatomic, strong) NSArray *food;
@property (nonatomic, strong) NSArray *health;
@end
// ***** Type.m file ***** //
@implementation Type
+ (NSDictionary*)mts_mapping
{
return @{@"food": mts_key(food),
@"Health": mts_key(health),
};
}
+ (NSDictionary*)mts_arrayClassMapping
{
return @{mts_key(food): Subtype.class,
mts_key(health): Subtype.class,
};
}
@end
// ***** Subtype.h file ***** //
@interface Subtype: NSObject
@property (nonatomic, assign) NSInteger cve;
@property (nonatomic, copy) NSString *theDescription;
@end
// ***** Subtypes.m file ***** //
@implementation Subtype
+ (NSDictionary*)mts_mapping
{
return @{@"cve": mts_key(cve),
@"description": mts_key(theDescription),
};
}
@end
// Get the json data
NSDictionary * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
// Extract the JSON dictionary of types.
NSDictionary *jsonType = [jsonObject objectForKey:@"Types"];
// Create a Type object
Type *type = [[Type alloc] init];
// Map JSON contents to the type object with Motis
[type mts_setValuesForKeysWithDictionary:jsonType];