JSONExport 工具
JSONExport 是把JSONExport 的格式文件转换成为Model 对象的工具。
下面是JSONExport gitHub地址:
一.安装方法:
1.下载代码
image-201910081104081082.用XCode打开工程
image-201910081105294153.编译工程:Command+B
image-201910081107136094.找到软件包:product 文件夹下的包文件,右键,Show in Finder
image-201910081108562855.拖入应用程序
image-20191008111128652二.使用:
1.解析,将下面的json 文字复制到json data 如图
{
"name":"李四",
"age":20,
"cars":["宝马车","路虎车","保时捷"],
"cer":{"Compuer":"l1","Enlish":"4"},
"address":"河南"
}
image-20191008123239793
说明:
1.Person 是RootClass 的名字,
2.FZ 是所有类的前缀,
3.FZBaseModel是所有类的父亲(或者叫基类吧)。
4.是选择开发语言
2.构造方法 Constructor
注意:选中Constructor的时候,会有构造方法,没选中没有构造方法。
2.1选中Constructor:
代码如下:
.h代码:
#import <UIKit/UIKit.h>
#import "FZBaseModel.h"
#import "FZCer.h"
@interface FZPerson : FZBaseModel
@property (nonatomic, strong) NSString * address;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray * cars;
@property (nonatomic, strong) FZCer * cer;
@property (nonatomic, strong) NSString * name;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
.m 代码
//
// FZPerson.m
// Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport
#import "FZPerson.h"
NSString *const kFZPersonAddress = @"address";
NSString *const kFZPersonAge = @"age";
NSString *const kFZPersonCars = @"cars";
NSString *const kFZPersonCer = @"cer";
NSString *const kFZPersonName = @"name";
@interface FZPerson ()
@end
@implementation FZPerson
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
self = [super init];
if(![dictionary[kFZPersonAddress] isKindOfClass:[NSNull class]]){
self.address = dictionary[kFZPersonAddress];
}
if(![dictionary[kFZPersonAge] isKindOfClass:[NSNull class]]){
self.age = [dictionary[kFZPersonAge] integerValue];
}
if(![dictionary[kFZPersonCars] isKindOfClass:[NSNull class]]){
self.cars = dictionary[kFZPersonCars];
}
if(![dictionary[kFZPersonCer] isKindOfClass:[NSNull class]]){
self.cer = [[FZCer alloc] initWithDictionary:dictionary[kFZPersonCer]];
}
if(![dictionary[kFZPersonName] isKindOfClass:[NSNull class]]){
self.name = dictionary[kFZPersonName];
}
return self;
}
@end
3.实用方法方法 Utility methods
选中会有如下三个方法,如果没选中,就没有这四个方法
-(NSDictionary *)toDictionary;
- (void)initWithCoder:(NSCoder *)aCoder;
- (void)encodeWithCoder:(NSCoder *)aCoder
- (instancetype)copyWithZone:(NSZone *)zone
1.toDictionary model 转化为字典。
2.initWithCoder model 归档方法
3.encodeWithCoder model 解归档方法
4.copyWithZone model在 copy 的时候使用
image-20191008115829983代码如下:
.h代码
#import <UIKit/UIKit.h>
#import "FZBaseModel.h"
#import "FZCer.h"
@interface FZPerson : FZBaseModel
@property (nonatomic, strong) NSString * address;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray * cars;
@property (nonatomic, strong) FZCer * cer;
@property (nonatomic, strong) NSString * name;
-(NSDictionary *)toDictionary;
@end
.m代码
//
// FZPerson.m
// Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport
#import "FZPerson.h"
NSString *const kFZPersonAddress = @"address";
NSString *const kFZPersonAge = @"age";
NSString *const kFZPersonCars = @"cars";
NSString *const kFZPersonCer = @"cer";
NSString *const kFZPersonName = @"name";
@interface FZPerson ()
@end
@implementation FZPerson
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
-(NSDictionary *)toDictionary
{
NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
if(self.address != nil){
dictionary[kFZPersonAddress] = self.address;
}
dictionary[kFZPersonAge] = @(self.age);
if(self.cars != nil){
dictionary[kFZPersonCars] = self.cars;
}
if(self.cer != nil){
dictionary[kFZPersonCer] = [self.cer toDictionary];
}
if(self.name != nil){
dictionary[kFZPersonName] = self.name;
}
return dictionary;
}
/**
* Implementation of NSCoding encoding method
*/
/**
* Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
*/
- (void)encodeWithCoder:(NSCoder *)aCoder
{
if(self.address != nil){
[aCoder encodeObject:self.address forKey:kFZPersonAddress];
}
[aCoder encodeObject:@(self.age) forKey:kFZPersonAge]; if(self.cars != nil){
[aCoder encodeObject:self.cars forKey:kFZPersonCars];
}
if(self.cer != nil){
[aCoder encodeObject:self.cer forKey:kFZPersonCer];
}
if(self.name != nil){
[aCoder encodeObject:self.name forKey:kFZPersonName];
}
}
/**
* Implementation of NSCoding initWithCoder: method
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
self.address = [aDecoder decodeObjectForKey:kFZPersonAddress];
self.age = [[aDecoder decodeObjectForKey:kFZPersonAge] integerValue];
self.cars = [aDecoder decodeObjectForKey:kFZPersonCars];
self.cer = [aDecoder decodeObjectForKey:kFZPersonCer];
self.name = [aDecoder decodeObjectForKey:kFZPersonName];
return self;
}
/**
* Implementation of NSCopying copyWithZone: method
*/
- (instancetype)copyWithZone:(NSZone *)zone
{
FZPerson *copy = [FZPerson new];
copy.address = [self.address copy];
copy.age = self.age;
copy.cars = [self.cars copy];
copy.cer = [self.cer copy];
copy.name = [self.name copy];
return copy;
}
@end
网友评论