JSONModel

1.基于名称的自动映射

{
    "id": 123,
    "name": "Product name",
    "price": 12.95
}

@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@end

2.模型级联(模型包括其他模型)

{
    "orderId": 104,
    "totalPrice": 13.45,
    "product": {
        "id": 123,
        "name": "Product name",
        "price": 12.95
    }
}

@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@end

@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) float totalPrice;
@property (nonatomic) ProductModel *product;
@end

3. 模型集合

{
    "orderId": 104,
    "totalPrice": 103.45,
    "products": [
        {
            "id": 123,
            "name": "Product #1",
            "price": 12.95
        },
        {
            "id": 137,
            "name": "Product #2",
            "price": 82.95
        }
    ]
}

@protocol ProductModel;

@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@end

@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) float totalPrice;
@property (nonatomic) NSArray  *products;
@end

4.嵌套键映射

{
    "orderId": 104,
    "orderDetails": {
        "name": "Product #1",
        "price": {
            "usd": 12.95
        }
    }
}
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *productName;
@property (nonatomic) float price;
@end

@implementation OrderModel

+ (JSONKeyMapper *)keyMapper
{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
        @"id": @"orderId",
        @"productName": @"orderDetails.name",
        @"price": @"orderDetails.price.usd"
    }];
}

@end

5.字段中带下划线

{
    "order_id": 104,
    "order_product": "Product #1",
    "order_price": 12.95
}

@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) NSString *orderProduct;
@property (nonatomic) float orderPrice;
@end

@implementation OrderModel

+ (JSONKeyMapper *)keyMapper
{
    return [JSONKeyMapper mapperForSnakeCase];
}

@end

6. 可选属性(即可以缺失或为空)

{
    "id": 123,
    "name": null,
    "price": 12.95
}

@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString  *name;
@property (nonatomic) float price;
@property (nonatomic) NSNumber  *uuid;
@end

7.忽略的属性(即JSONModel完全忽略它们)

{
    "id": 123,
    "name": null
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString  *customProperty;
@end

8.标量类型可选

{
    "id": null
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@end

@implementation ProductModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
    if ([propertyName isEqualToString:@"id"])
        return YES;

    return NO;
}
// 如果所有属性均可为空的话
+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
    return YES;
}
@end

9.将模型导出为NSDictionary或JSON

ProductModel *pm = [ProductModel new];
pm.name = @"Some Name";

// convert to dictionary
NSDictionary *dict = [pm toDictionary];

// convert to json
NSString *string = [pm toJSONString];

你可能感兴趣的:(JSONModel)