JSONModel的使用
2019-02-11 本文已影响3人
coder小鹏
之前有写过一篇关于如何使用
YYModel
的文章,最近刚好有空,打算把JSONMOdel
的使用也总结一下,接下来就是一些关于JSONModel
的简单使用以及使用中的一些问题的总结,如果想看关于YYModel
的使用,请点击查看
JSON
转换为模型的例子
//JSON
{ "country": "Germany", "dialCode": 49, "isInEurope": true }
//Model
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface CountryModel : JSONModel
@property (nonatomic, copy) NSString *country;
@property (nonatomic, copy) NSString *dialCode;
@property (nonatomic, assign) BOOL isInEurope;
@end
NS_ASSUME_NONNULL_END
//JSON转换为模型
NSError *error;
CountryModel *countryModel = [[CountryModel alloc] initWithDictionary:dic error:&error];
模型转换为字典
//将模型快速转换为字典
NSDictionary *dict = [countryModel toDictionary];
模型转换为字符串
//将模型快速转换为字符串
NSString *string = [countryModel toJSONString];
模型套模型的数据解析
//JSON
"orderId": 104,
"totalPrice": 13.45,
"product": {
"id": 123,
"name": "Product name",
"price": 12.95
}
//Model
//.h
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface ProductModel : JSONModel
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) float price;
@end
@interface OtherModel : JSONModel
@property (nonatomic, assign) NSInteger orderId;
@property (nonatomic, assign) float totalPrice;
@property (nonatomic, strong) ProductModel *product;
@end
NS_ASSUME_NONNULL_END
//.m
#import "ProductModel.h"
@implementation ProductModel
@end
@implementation OtherModel
@end
//模型套模型的例子
NSDictionary *dic = @{
@"orderId": @(104),
@"totalPrice": @(13.45),
@"product": @{
@"id": @(123),
@"name": @"Product name",
@"price": @(12.95)
}
};
NSError *error;
OtherModel *model = [[OtherModel alloc] initWithDictionary:dic error:&error];
注意:在解析的过程中,需保证模型中的键和要解析的键名称保持一致,否则无法解析
模型集合
//JSON
{
"order_id": 104,
"total_price": 103.45,
"products" : [
{
"id": "123",
"name": "Product #1",
"price": 12.95
},
{
"id": "137",
"name": "Product #2",
"price": 82.95
}
]
}
//Model
//.h
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface ProductModel : JSONModel
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) float price;
@end
@interface OtherModel : JSONModel
@property (nonatomic, assign) NSInteger orderId;
@property (nonatomic, assign) float totalPrice;
@property (nonatomic, strong) NSArray<ProductModel *>* products;
@end
NS_ASSUME_NONNULL_END
//.m
#import "ProductModel.h"
@implementation ProductModel
@end
@implementation OtherModel
@end
//模型集合
NSDictionary *dic = @{
@"orderId": @(104),
@"totalPrice": @(13.45),
@"products": @[@{
@"id": @(123),
@"name": @"Product name",
@"price": @(12.95)},
@{@"id": @(134),
@"name": @"Product asasask",
@"price": @(12.05)}
]
};
NSError *error;
OtherModel *model = [[OtherModel alloc] initWithDictionary:dic error:&error];
设置下划线自动转驼峰
//JSON
{
"order_id": 104,
"order_product" : @"Product#1",
"order_price" : 12.95
}
//Model
//.h
#import "JSONModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface OrderModel : JSONModel
@property (assign, nonatomic) int orderId;
@property (assign, nonatomic) float orderPrice;
@property (strong, nonatomic) NSString* orderProduct;
@end
NS_ASSUME_NONNULL_END
//.m
+(JSONKeyMapper*)keyMapper
{
return [JSONKeyMapper mapperForSnakeCase];
}
//设置下划线自动转驼峰
NSDictionary *dic = @{
@"order_id": @(104),
@"order_product" : @"Product#1",
@"order_price" : @(12.95)
};
NSError *error;
OrderModel *model = [[OrderModel alloc] initWithDictionary:dic error:&error];
可选属性 (就是说这个属性可以为null
或者为空)
//JSON
{
"id": "123",
"name": null,
"price": 12.95
}
//Model
@interface ProductModel : JSONModel
@property (nonatomic, copy) NSString <Optional>*name;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) float price;
@property (strong, nonatomic) NSNumber<Optional>* uuid;
@end
//可选属性(就是说这个属性可以为null或者为空)
NSDictionary *dic = @{
@"id": @"123",
@"name": [NSNull null],
@"price": @(12.95)
};
NSError *error;
ProductModel *model = [[ProductModel alloc] initWithDictionary:dic error:&error];
忽略属性 (就是完全忽略这个属性)
//JSON
{
"id": "123",
"name": [NSNull null],
"price": (12.95)
}
//Model
@interface ProductModel : JSONModel
@property (nonatomic, copy) NSString <Optional>*name;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) float price;
@property (strong, nonatomic) NSNumber<Optional>* uuid;
@property (strong, nonatomic) NSString<Ignore>* customProperty;
@end
//忽略属性 (就是完全忽略这个属性)
NSDictionary *dic = @{
@"id": @"123",
@"name": [NSNull null],
@"price": @(12.95)
};
NSError *error;
ProductModel *model = [[ProductModel alloc] initWithDictionary:dic error:&error];
设置所有的属性为可选(所有属性值可以为空)
@implementation ProductModel
+(BOOL)propertyIsOptional:(NSString*)propertyName
{
return YES;
}
@end
自定义JSON
校验
//JSON
"id": "123",
"name": "dfghjkl;",
"price": (12.95)
//Model
//.h
@interface ProductModel : JSONModel
@property (nonatomic, copy) NSString <Optional>*name;
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) float price;
@property (strong, nonatomic) NSNumber<Optional>* uuid;
@property (strong, nonatomic) NSString<Ignore>* customProperty;
@end
//.m
- (BOOL)validate:(NSError *__autoreleasing *)error
{
BOOL valid = [super validate:error];
if (self.name.length < 2)
{
*error = [NSError errorWithDomain:@"me.mycompany.com" code:1 userInfo:nil];
valid = NO;
}
return valid;
}
//自定义JSON校验
NSDictionary *dic = @{
@"id": @"123",
@"name": @"dfghjkl;",
@"price": @(12.95)
};
NSError *error;
ProductModel *model = [[ProductModel alloc] initWithDictionary:dic error:&error];
注意:如果不满足条件,则转换模型失败
Custom getters/setters
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@property (nonatomic) NSLocale *locale;
@end
@implementation ProductModel
- (void)setLocaleWithNSString:(NSString *)string
{
self.locale = [NSLocale localeWithLocaleIdentifier:string];
}
- (void)setLocaleWithNSDictionary:(NSDictionary *)dictionary
{
self.locale = [NSLocale localeWithLocaleIdentifier:dictionary[@"identifier"]];
}
- (NSString *)JSONObjectForLocale
{
return self.locale.localeIdentifier;
}
@end
至此,关于
JSONModel
的相关用法已经全部说完了,如果由哪些说的不对的地方,欢迎指正。如果喜欢,欢迎点赞关注!!!