JSON序列化为实体对象

1、JSON 文本,文件名 applications.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];
}

 

转载于:https://www.cnblogs.com/allanliu/p/4606857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值