JSONModel的基本使用

JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它。

使用前准备

添加 JSONModel 到你的工程中

1、需要的环境:

  • ARC,iOS 5.0+ / OSX 10.7 +
  • 引入框架SystemConfiguration.framework

2、获取途径:

  1)、通过源文件获取
  * 下载 JSONModel zip包
  *  将 JSONModel 文件夹拷贝到你的工程项目中
  *  将库 SystemConfiguration.framework 添加上         
  2)、使用 Cocoa pods 来安装:
      pod 'JSONModel'

3、源码的文档:

源码本身包含了类的文档,你可以自己编译后导入到你的Xcode中:

  • 如果你还没安装 appledoc ,先安装 appledoc
  • 在Xcode上键入 appledoc 安装文档,在根目录下
  • 重启Xcode

基本使用

假设你的 JSON 串像下面这样子:

{"id":"10", "country":"Germany", "dialCode": 49, "isInEurope":true}
  • 创建一个你自己的类,并继承至 JSONModel
  • 在你的头文件里面进行声明你所需要的 JSON key值
#import "JSONModel.h"

@interface CountryModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope;

@end

.m文件中你不需要做其他的事情了.

初始化你的 model ,如下所示:

#import "CountryModel.h"
...

NSString* json = (fetch here JSON from Internet) ... 
NSError* err = nil;
CountryModel* country = [[CountryModel alloc] initWithString:json error:&err];

如果传过来的 JSON 合法,你所定义的所有的属性都会与该 JSON 值相匹配,并且 JSONModel 也会尝试尽可能的转换成你所想要的数据,就像上面的例子:

  • 转化 "id",从字符串转换成 int 型
  • 拷贝 country 属性的值
  • 转换 dialCode ,从NSNumber 转换为 NSString 值
  • 最后一个呢是将 isInEurope 转换成 BOOL 的属性

所以,你需要做的就是定义出你期望的属性就行了。

例子

命名自动匹配

{
  "id": "123",
  "name": "Product name",
  "price": 12.95
}
@interface ProductModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;

@end

@implementation ProductModel

@end

model中含有其他的model

{
  "order_id": 104,
  "total_price": 13.45,
  "product" : {
    "id": "123",
    "name": "Product name",
    "price": 12.95
  }
}
@interface OrderModel : JSONModel

@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) ProductModel* product;

@end

@implementation OrderModel

@end

model中含有其他model的集合

{
  "order_id": 104,
  "total_price": 103.45,
  "products" : [
    {
      "id": "123",
      "name": "Product #1",
      "price": 12.95
    },
    {
      "id": "137",
      "name": "Product #2",
      "price": 82.95
    }
  ]
}
@protocol ProductModel

@end

@interface ProductModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;

@end

@implementation ProductModel

@end

@interface OrderModel : JSONModel

@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) NSArray<ProductModel>* products;

@end

@implementation OrderModel

@end

键值转回匹配

{
  "order_id": 104,
  "order_details" : [
    {
      "name": "Product#1",
      "price": {
        "usd": 12.95
      }
    }
  ]
}
@interface OrderModel : JSONModel

@property (assign, nonatomic) int id;
@property (assign, nonatomic) float price;
@property (strong, nonatomic) NSString* productName;

@end

@implementation OrderModel

+ (JSONKeyMapper*)keyMapper
{
     return [[JSONKeyMapper alloc] initWithDictionary:@{
    @"order_id": @"id",
    @"order_details.name": @"productName",
    @"order_details.price.usd": @"price"
  }];
}

@end

设置全局的键值转回匹配

[JSONModel setGlobalKeyMapper:[
    [JSONKeyMapper alloc] initWithDictionary:@{
      @"item_id":@"ID",
      @"item.name": @"itemName"
   }]
];

将下滑线转换成首字母大写

{
  "order_id": 104,
  "order_product" : @"Product#1",
  "order_price" : 12.95
}
@interface OrderModel : JSONModel

@property (assign, nonatomic) int orderId;
@property (assign, nonatomic) float orderPrice;
@property (strong, nonatomic) NSString* orderProduct;

@end

@implementation OrderModel

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

@end

可以为空的属性值

{
  "id": "123",
  "name": null,
  "price": 12.95
}
@interface ProductModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString<Optional>* name;
@property (assign, nonatomic) float price;
@property (strong, nonatomic) NSNumber<Optional>* uuid;

@end

@implementation ProductModel

@end

忽略某些属性

{
  "id": "123",
  "name": null
}
@interface ProductModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString<Ignore>* customProperty;

@end

@implementation ProductModel

@end

让所有的属性都可以有空的属性值

@implementation ProductModel

+ (BOOL)propertyIsOptional:(NSString*)propertyName
{
     return YES;
}

@end

将集合元素转换成 model

{
  "order_id": 104,
  "total_price": 103.45,
  "products" : [
    {
      "id": "123",
      "name": "Product #1",
      "price": 12.95
    },
    {
      "id": "137",
      "name": "Product #2",
      "price": 82.95
    }
  ]
}
@protocol ProductModel

@end

@interface ProductModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;

@end

@implementation ProductModel

@end

@interface OrderModel : JSONModel

@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) NSArray<ProductModel, ConvertOnDemand>* products;

@end

@implementation OrderModel

@end
posted @ 2015-10-08 17:39  求真求道  阅读(558)  评论(0编辑  收藏  举报