iOS开发 Jastor的简单使用

  • Jastor简介。
            jastor是一个基于oc运行时的库【1】,它可以将字典对象转换成NSObject对象。它支持NSString, NSNumber,NSArray, NSDictionary以及它们的嵌套类型。例如现在需要将dict转换为model。 dict为NSDictionary对象, model为继承于Jastor的NSObject对象。 Jastor通过遍历model对象的所有属性, 以属性作的名字作为dict的key,得到字典的value值。 然后根据value值的类型,进行不同的处理。 如果value是NSArray类型,那么就进入NSArray的解析流程; 如果value是NSDictionary类型,那么开始进行递归调用,如果是其他普通类型,则直接setvalue:forKey。当进入NSArray的处理流程时候,调用类的“属性_class”方法获取到数组对象的类型,然后对数组对象进行逐步解析,数组对象也必须是字典类型。
  • Jastor原理。

Jastor库总共有两个文件Jastor【2】、JastorRuntimeHelper,Jastor是用来解析Json,JastorRuntimeHelper是在解析过程中提供runtime支持。

1.Jastor提供了四个方法:

+ (id)objectFromDictionary:(NSDictionary*)dictionary;//解析Json的方法1

- (id)initWithDictionary:(NSDictionary *)dictionary;//解析Json的方法2

- (NSMutableDictionary *)toDictionary;//将自己转换成Dictionary

- (NSDictionary *)map;//修改属性名

其中,initWithDictionary:和objectFromDictionary:都是用来解析Json的方法。objectFromDictionary:是类方法,在alloc之后直接调用initWithDictionary:方法来解析Json字典

         2.JastorRuntimeHelper提供了三个方法:

+ ( BOOL )isPropertyReadOnly:(Class)klass propertyName:( NSString *)propertyName; //判断model的属性是否为只读
+ (Class)propertyClassForPropertyName:( NSString *)propertyName ofClass:(Class)klass; //获取Model的propertyName的类

+ (NSArray *)propertyNames:(Class)klass;//获取model的所以属性名

主要用到了runtime.h的几个相应的方法,在运行时获取当前model类的属性相关信息。

  • Jastor的使用。
         1.让Model继承自Jastor并引入Jastor头文件。
             
         2.按接口文档声明Model的属性(实现文件中不用实现任何代码)。
      @interface JXRAreaModel :Jastor

      @property(nonatomic,copy)NSString *codeId;//节点id

      @property(nonatomic,copy)NSString *codeName;//节点名称

      @property(nonatomic,copy)NSString *codeValue;//节点编号

      @property(nonatomic,assign)short codeType;//节点类型(1-省;2-市;3-)

      @property(nonatomic,copy)NSString * parentCode;//父节点编码

      @end


        3. 解析数据:使用Jastor的 initWithDictionary : 方法即可按该Model的属性自动解析。

     JXRAreaModel *model = [[JXRAreaModelalloc]initWithDictionary:dic];


    其中dic常为以下格式类型的NSDictionary型数据(即单层无嵌套字典):
{
"codeId": "ho889ijnbgfr456tfc4321qa456tfgbn",
"codeName": "福建省",
"codeValue": "31000",
"codeType": "1",
"parentCode": ""
}

    如果dic为嵌套类型字典如:
{
    "goodsList": [
        {
            "goodsId": "00010001aaaaaaaaaaaaaaaaaaaGoods",
            "goodsImage": "/images/goodsImages/0101iphone6_1.png",
            "goodsName": "iphone6"
        },
        {
            "goodsId": "00020001aaaaaaaaaaaaaaaaaaaGoods",
            "goodsImage": "/images/goodsImages/0201qiCheZuLin_1.png",
            "goodsName": "iphone7"
        }
    ],
    "shopList": [
        {
            "shopId": "084fa05bc7e24e9783ee0f8250e7f8df",
            "shopLat": 0,
            "shopLong": 0
        },
        {
            "shopId": "15a492ae5cfc46018b741dbadc7d1f24",
            "shopLat": 0,
            "shopLong": 0
        }
    ]
}
    则改JXRAreaModel的属性可设为:

    @property(nonatomic,retain)NSMutableArray * shopList;//店铺数组(Shop)

    @property(nonatomic,retain)NSMutableArray * goodsList;//商品数组(Goods)

    其中shopList与goodsList里面存储的数据又可设为Model类型,如Shop和Goods。
    Shop的属性列表为:

    @property(nonatomic,strong)NSString *shopId;//店铺id

    @property(nonatomic,assign)float shopLong;//店铺位置经度

    @property(nonatomic,assign)float shopLat;//店铺位置纬度    

    Goods的属性列表为:

    @property (nonatomic,strong)NSString *goodsId;//商品id

    @property (nonatomic,strong)NSString *goodsImage;//商品图片

    @property (nonatomic,strongNSString *goodsName;//商品名称     
    则JXRAreaModel的实现文件中只要实现如下方法即可让shopList与goodsList里的数据按Shop和Goods的属性自动解析。

    + (Class)shopList_class {

        return [Shop class];

    }

    + (Class)goodsList_class {

        return [Goods class];

    }  

    其中方法名为"属性名+_class",返回Model类。

  • 另。
   1.若服务器返回的数据中有字段与系统关键字冲突。如:"id": "a3d55f0a36b9b303b08fe80b517ad21a"
     则属性不可设为id,应使用与系统不冲突字符串替换该属性。如:

@property(nonatomic,copy)NSString *userId;//用户id     

     并在实现文件中实现如下方法:

- (NSDictionary *)map

{

    NSMutableDictionary *map = [NSMutableDictionarydictionaryWithDictionary:[supermap]];

    [map setObject:@"id"forKey:@"userId"];

    return [NSDictionarydictionaryWithDictionary:map];

}

【1】 object-c编程tips-jastor自动解析 http://www.th7.cn/Program/IOS/201408/256884.shtml


猜你喜欢

转载自blog.csdn.net/devil_0311/article/details/53994372
今日推荐