之前有寫過一篇關(guān)于如何使用
YYModel
的文章,最近剛好有空,打算把JSONMOdel
的使用也總結(jié)一下丽柿,接下來就是一些關(guān)于JSONModel
的簡單使用以及使用中的一些問題的總結(jié),如果想看關(guān)于YYModel
的使用,請點擊查看
JSON
轉(zhuǎn)換為模型的例子
//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轉(zhuǎn)換為模型
NSError *error;
CountryModel *countryModel = [[CountryModel alloc] initWithDictionary:dic error:&error];
模型轉(zhuǎn)換為字典
//將模型快速轉(zhuǎn)換為字典
NSDictionary *dict = [countryModel toDictionary];
模型轉(zhuǎn)換為字符串
//將模型快速轉(zhuǎn)換為字符串
NSString *string = [countryModel toJSONString];
模型套模型的數(shù)據(jù)解析
//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];
設(shè)置下劃線自動轉(zhuǎn)駝峰
//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];
}
//設(shè)置下劃線自動轉(zhuǎn)駝峰
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];
設(shè)置所有的屬性為可選(所有屬性值可以為空)
@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];
注意:如果不滿足條件癌压,則轉(zhuǎn)換模型失敗
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
至此仰泻,關(guān)于
JSONModel
的相關(guān)用法已經(jīng)全部說完了,如果由哪些說的不對的地方滩届,歡迎指正集侯。如果喜歡,歡迎點贊關(guān)注!!!