JSONModel的使用

参考自:https://github.com/jsonmodel/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 <ProductModel> *products;
@end
复制代码

或者

@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) float totalPrice;
@property (nonatomic) NSArray<ProductModel *> <ProductModel> *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 <Optional> *name;
@property (nonatomic) float price;
@property (nonatomic) NSNumber <Optional> *uuid;
@end

 

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

{
    "id": 123,
    "name": null
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString <Ignore> *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;
}

@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];

 

posted @   liuw_flexi  阅读(5860)  评论(0编辑  收藏  举报
编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 用 C# 插值字符串处理器写一个 sscanf
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
历史上的今天:
2017-04-24 首次用第三方登录+绑定手机与直接用手机注册有什么区别?
点击右上角即可分享
微信分享提示