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];
此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.
我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
2017-04-24 首次用第三方登录+绑定手机与直接用手机注册有什么区别?