YYModel 사용
iOS
개발 에 있어 서 각종 JSON
과 모델 이 서로 전환 하 는 수 요 를 사용 할 수 있 습 니 다. 이전 프로젝트 에서 계속 사용 MJExtension
했 지만 최근 에 경량급 라 이브 러 리 YYModel
를 발 견 했 습 니 다. 사용 이 간단 하고 성능 도 좋 습 니 다. 다음은 YYModel
의 간단 한 용법 과 주의사항 을 말씀 드 리 겠 습 니 다.JSON
모델 로 전환 하거나 모델 이 JSON
로 전환 하 는 예 //JSON
{
accountId = "";
avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
unionId = o2t87wKAR1pNRZR0cnReBT608mok;
userid = 11703346;
wxNickName = "\U90a3\U5496";
}
//Model
@interface MyFansModel : NSObject
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@end
//JSON
MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
MyFansModel *fansModel = [MyFansModel yy_modelWithDictionary:dic];
// JSON
NSDictionary *json = [fansModel yy_modelToJSONObject];
//JSON
{ "merchantId" = "";
"avatar" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
"unionID" = "o2t87wKAR1pNRZR0cnReBT608mok";
"userID" = "11703346";
"userName" = " ";
}
//Model
//.h
#import
@interface MyFansModel : NSObject
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@end
//.m
+ (nullable NSDictionary *)modelCustomPropertyMapper
{
return @{@"avatarUrl" : @"avatar",
@"wxNickName" : @"userName",
@"accountId" : @"merchantId",
@"unionId" : @"unionID",
@"userid":@"userID",
};
}
MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
MyFansModel *fansModel = [MyFansModel yy_modelWithDictionary:dic];
//JSON
{
citys = (
{
area = "";
areaId = "";
areas = (
{
area = "\U4e1c\U57ce\U533a";
cityid = 110100;
id = 110101;
status = 1;
},
{
area = "\U897f\U57ce\U533a";
cityid = 110100;
id = 110102;
status = 1;
},
{
area = "\U5d07\U6587\U533a";
cityid = 110100;
id = 110103;
status = 1;
},
{
area = "\U5ba3\U6b66\U533a";
cityid = 110100;
id = 110104;
status = 1;
},
{
area = "\U671d\U9633\U533a";
cityid = 110100;
id = 110105;
status = 1;
},
{
area = "\U4e30\U53f0\U533a";
cityid = 110100;
id = 110106;
status = 1;
},
{
area = "\U77f3\U666f\U5c71\U533a";
cityid = 110100;
id = 110107;
status = 1;
},
{
area = "\U6d77\U6dc0\U533a";
cityid = 110100;
id = 110108;
status = 1;
},
{
area = "\U95e8\U5934\U6c9f\U533a";
cityid = 110100;
id = 110109;
status = 1;
},
{
area = "\U623f\U5c71\U533a";
cityid = 110100;
id = 110111;
status = 1;
},
{
area = "\U901a\U5dde\U533a";
cityid = 110100;
id = 110112;
status = 1;
},
{
area = "\U987a\U4e49\U533a";
cityid = 110100;
id = 110113;
status = 1;
},
{
area = "\U660c\U5e73\U533a";
cityid = 110100;
id = 110114;
status = 1;
},
{
area = "\U5927\U5174\U533a";
cityid = 110100;
id = 110115;
status = 1;
},
{
area = "\U6000\U67d4\U533a";
cityid = 110100;
id = 110116;
status = 1;
},
{
area = "\U5e73\U8c37\U533a";
cityid = 110100;
id = 110117;
status = 1;
}
);
city = "\U5e02\U8f96\U533a";
cityId = "";
id = 110100;
province = "";
provinceid = 110000;
status = 1;
},
{
area = "";
areaId = "";
areas = (
{
area = "\U5bc6\U4e91\U53bf";
cityid = 110200;
id = 110228;
status = 1;
},
{
area = "\U5ef6\U5e86\U53bf";
cityid = 110200;
id = 110229;
status = 1;
}
);
city = "\U53bf";
cityId = "";
id = 110200;
province = "";
provinceid = 110000;
status = 1;
}
);
id = 110000;
province = "\U5317\U4eac\U5e02";
status = 1;
}
//Model
//.h
//BaseModel id _id
#import
NS_ASSUME_NONNULL_BEGIN
@interface BaseModel : NSObject
/// id _id
@property (nonatomic, copy) NSString *_id;
@end
//.m
+(NSDictionary *)modelCustomPropertyMapper
{
return @{@"_id": @"id"};
}
// .h
@interface AreaModel : BaseModel
@property (nonatomic, copy) NSString *area;
@property (nonatomic, copy) NSString *cityid;
@property (nonatomic, copy) NSString *status;
@end
@interface CityModel : BaseModel
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *provinceid;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, strong) NSArray *areas;
@end
@interface ProvinceModel : BaseModel
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, strong) NSArray *citys;
@end
//.m
@implementation AreaModel
@end
@implementation CityModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"areas" : [AreaModel class]};
}
@end
@implementation ProvinceModel
+ (NSDictionary *)modelContainerPropertyGenericClass {
return @{@"citys" : [CityModel class]};
}
@end
// JSON
ProvinceModel *provinceModel = [ProvinceModel yy_modelWithJSON:dic];
CityModel *cityModel = provinceModel.citys[0];
AreaModel *areaModel = cityModel.areas[0];
//JSON
(
{
"accountId" = "";
"avatarUrl" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
"unionId" = "o2t87wKAR1pNRZR0cnReBT608mok";
"userid" = "11703346",
"wxNickName" = " "
},
{
"accountId" = "";
"avatarUrl" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
"unionId" = "o2t87wKAR1pNRZR0cnReBT608mok";
"userid" = "11293020";
"wxNickName" = " ";
}
);
//Model
@interface MyFansModel : NSObject
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@end
//
NSArray *myFansModelArray = [NSArray yy_modelArrayWithClass:[MyFansModel class] json:array];
//JSON
{
"schoolName" = " ";
"teachers" = (
{"teacherName"= "Sndday","teacherAge" = 50};
{"teacherName" = "Ssasas","teacherAge" = 70};
{"teacherName" = "Snddasa","teacherAge"= 30)}
);
};
//Model
//.h
#import
@interface SchoolModel : NSObject
@property (nonatomic, copy) NSString *schoolName;
@property (nonatomic, copy) NSArray *teachers;
@end
//.m
#import "TeacherModel.h"
@implementation SchoolModel
+ (nullable NSDictionary *)modelContainerPropertyGenericClass
{
return @{@"teachers":[TeacherModel class]};
}
@end
//TeacherModel
@interface TeacherModel : NSObject
@property (nonatomic, copy) NSString *teacherName;
@property (nonatomic, assign) NSInteger teacherAge;
@end
//
NSDictionary *dic = @{
@"schoolName":@" ",
@"teachers":@[
@{@"teacherName":@"Sndday",@"teacherAge":@(50)},
@{@"teacherName":@"Ssasas",@"teacherAge":@(70)},
@{@"teacherName":@"Snddasa",@"teacherAge":@(30)}
],
};
SchoolModel *model = [SchoolModel yy_modelWithJSON:dic];
TeacherModel *teacherModel = model.teachers[0];
//JSON
{
accountId = "";
avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
unionId = o2t87wKAR1pNRZR0cnReBT608mok;
userid = 11703346;
wxNickName = "\U90a3\U5496";
}
//Model
//.h
#import
@interface MyFansModel : NSObject
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@end
//.m
+ (NSArray *)modelPropertyBlacklist
{
return @[@"avatarUrl", @"accountId"];
}
// JSON , ```avatarUrl``` ```accountId``` ```nil```
MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
//JSON
{
accountId = "";
avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
unionId = o2t87wKAR1pNRZR0cnReBT608mok;
userid = 11703346;
wxNickName = "\U90a3\U5496";
}
//Model
//.h
#import
@interface MyFansModel : NSObject
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@end
//.m
+ (NSArray *)modelPropertyWhitelist
{
return @[@"avatarUrl", @"accountId"];
}
// JSON , ```avatarUrl``` ```accountId``` ```nil```
MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
{
accountId = "";
avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
unionId = o2t87wKAR1pNRZR0cnReBT608mok;
userid = 11703346;
wxNickName = "\U90a3\U5496";
}
#import
@interface MyFansModel : NSObject
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@property (nonatomic, strong) NSString *resultStr;
@end
//.m
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
NSString *timestamp = dic[@"accountId"];
if ([timestamp isEqualToString:@""]) return NO;
_resultStr = [NSString stringWithFormat:@" %@",timestamp];
return YES;
}
// JSON
MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
상기 예 는
accountId
에 대한 검 사 를 했 기 때문에 accountId
요구 에 부합 되 지 않 으 면 이 JSON
는 해석 되 지 않 을 것 이다. 물론 백 엔 드 에서 되 돌아 오 는 시간 스탬프 를 시간 문자열 로 바 꾸 는 등 사용자 정의 작업 도 할 수 있다.이로써
YYModel
에 관 한 용법 은 모두 말 했 으 며, 어떤 말 이 잘못 되 었 다 면 지적 을 환영 합 니 다.마음 에 드 시 면 좋아요 눌 러 주세요!!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.