- Jastor简介。
- 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的使用。
@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
JXRAreaModel *model = [[JXRAreaModelalloc]initWithDictionary:dic];
{
"codeId": "ho889ijnbgfr456tfc4321qa456tfgbn","codeName": "福建省","codeValue": "31000","codeType": "1","parentCode": ""
}
{"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}]}
@property(nonatomic,retain)NSMutableArray * shopList;//店铺数组(存Shop)
@property(nonatomic,retain)NSMutableArray * goodsList;//商品数组(存Goods)
@property(nonatomic,strong)NSString *shopId;//店铺id
@property(nonatomic,assign)float shopLong;//店铺位置经度
@property(nonatomic,assign)float shopLat;//店铺位置纬度
@property (nonatomic,strong)NSString *goodsId;//商品id
@property (nonatomic,strong)NSString *goodsImage;//商品图片
@property (nonatomic,strong) NSString *goodsName;//商品名称+ (Class)shopList_class {
return [Shop class];
}
+ (Class)goodsList_class {
return [Goods class];
}
- 另。
@property(nonatomic,copy)NSString *userId;//用户id
- (NSDictionary *)map
{
NSMutableDictionary *map = [NSMutableDictionarydictionaryWithDictionary:[supermap]];
[map setObject:@"id"forKey:@"userId"];
return [NSDictionarydictionaryWithDictionary:map];
}
- demo源代码文件下载地址:https://github.com/elado/jastor
- 参考文献:
【1】 object-c编程tips-jastor自动解析 http://www.th7.cn/Program/IOS/201408/256884.shtml【2】Jastor解析原理初探http://blog.csdn.net/wellenten/article/details/40620977