JSON을 솔리드 객체로 시리얼화하기
13046 단어 json
[
{
"display_name": "500px",
"developer_name": "500px",
"identifier": "471965292"
},
{
"display_name": "Airbnb",
"developer_name": "Airbnb, Inc.",
"identifier": "401626263"
},
{
"display_name": "AppStore",
"developer_name": "Apple, Inc.",
"identifier": ""
},
{
"display_name": "Camera",
"developer_name": "Apple, Inc.",
"identifier": ""
},
{
"display_name": "Dropbox",
"developer_name": "Dropbox, Inc.",
"identifier": "327630330"
},
{
"display_name": "Facebook",
"developer_name": "Facebook, Inc.",
"identifier": "284882215"
},
{
"display_name": "WWDC",
"developer_name": "Apple, Inc.",
"identifier": "640199958"
},
]
2. 새 Entity 객체 클래스
#import <Foundation/Foundation.h>
//JSON
typedef NS_ENUM(NSUInteger, ApplicationType) {
ApplicationType500px,
ApplicationTypeAirbnb,
ApplicationTypeAppstore,
ApplicationTypeCamera,
ApplicationTypeDropbox,
ApplicationTypeFacebook,
ApplicationTypeFancy,
ApplicationTypeFoursquare,
ApplicationTypeiCloud,
ApplicationTypeInstagram,
ApplicationTypeiTunesConnect,
ApplicationTypeKickstarter,
ApplicationTypePath,
ApplicationTypePinterest,
ApplicationTypePhotos,
ApplicationTypePodcasts,
ApplicationTypeRemote,
ApplicationTypeSafari,
ApplicationTypeSkype,
ApplicationTypeSlack,
ApplicationTypeTumblr,
ApplicationTypeTwitter,
ApplicationTypeVideos,
ApplicationTypeVesper,
ApplicationTypeVine,
ApplicationTypeWhatsapp,
ApplicationTypeWWDC
};
@interface Application : NSObject
@property (nonatomic, strong) NSString *displayName;
@property (nonatomic, strong) NSString *developerName;
@property (nonatomic, strong) NSString *identifier;
@property (nonatomic, strong) NSString *iconName;
@property (nonatomic) ApplicationType type;
- (instancetype)initWithDictionary:(NSDictionary *)dict;
@end
#import "Application.h"
@implementation Application
- (instancetype)initWithDictionary:(NSDictionary *)dict
{
if (!dict) {
return nil;
}
self = [super init];
if (self) {
self.displayName = [dict objectForKey:@"display_name"];
self.developerName = [dict objectForKey:@"developer_name"];
self.identifier = [dict objectForKey:@"identifier"];
}
return self;
}
- (void)setDisplayName:(NSString *)displayName
{
_displayName = displayName;
self.iconName = [[[NSString stringWithFormat:@"icon_%@", self.displayName] lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
self.type = applicationTypeFromString(self.displayName);
}
ApplicationType applicationTypeFromString(NSString *string)
{
//JSON
NSArray *arr = @[
@"500px",
@"Airbnb",
@"AppStore",
@"Camera",
@"Dropbox",
@"Facebook",
@"Fancy",
@"Foursquare",
@"iCloud",
@"Instagram",
@"iTunes Connect",
@"Kickstarter",
@"Path",
@"Pinterest",
@"Photos",
@"Podcasts",
@"Remote",
@"Safari",
@"Skype",
@"Slack",
@"Tumblr",
@"Twitter",
@"Videos",
@"Vesper",
@"Vine",
@"WhatsApp",
@"WWDC"
];
return (ApplicationType)[arr indexOfObject:string];
}
@end
3. JSON을 읽고 서열화하여 읽은 정보를 Entity 대상에 봉인한다.
#pragma mark - Serialization
//
- (void)serializeApplications
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"applications" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSArray *objects = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions|NSJSONWritingPrettyPrinted error:nil] mutableCopy];
self.applications = [NSMutableArray new];
for (NSDictionary *dictionary in objects) {
Application *app = [[Application alloc] initWithDictionary:dictionary];
[self.applications addObject:app];
}
}
4, 호출 서열화 방법
@interface MainViewController ()
// JSON
@property (nonatomic, strong) NSMutableArray *applications;
@end
@implementation MainViewController
- (void)awakeFromNib
{
[super awakeFromNib];
//
[self serializeApplications];
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콘텐츠 SaaS | JSON 스키마 양식 빌더Bloomreach Content를 위한 JSON Form Builder 맞춤형 통합을 개발합니다. 최근 Bloomreach Content SaaS는 내장 앱 프레임워크를 사용하여 혁신적인 콘텐츠 유형 필드를 구축할...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.