【iOS】json文件转成NSArray

本文介绍了在iOS项目中,如何将json文件转换为NSArray,以适应需求变化时的数据管理。通过示例代码展示了从json文件读取数据并转化为NSArray的过程,提供了一种相对于直接在控制器写死或使用plist更具灵活性的方法。

在项目中,因为需求的更迭,一个列表里的数据会不断的改变,有人会选择直接在控制器里用代码写死数组,需求更迭的时候,去控制器里增删改数组元素,有人会选择用plist来处理,我之前一直用的是plist方法,比前者的可操作性更强,这篇文章介绍的方法,跟plist的逻辑是一样的,只是把plist换成了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": "Fancy",

  "developer_name": "Thing Daemon, Inc.",

  "identifier": "407324335"

  },

  {

  "display_name": "Foursquare",

  "developer_name": "Foursquare Labs",

  "identifier": "306934924"

  },

  {

  "display_name": "iCloud",

  "developer_name": "Apple, Inc.",

  "identifier": ""

  },

  {

  "display_name": "Instagram",

  "developer_name": "Instagram, Inc.",

  "identifier": "389801252"

  },

  {

  "display_name": "iTunes Connect",

  "developer_name": "Apple, Inc.",

  "identifier": "376771144"

  },

  {

  "display_name": "Kickstarter",

  "developer_name": "Kickstarter, Inc.",

  "identifier": "596961532"

  },

  {

  "display_name": "Path",

  "developer_name": "Path, Inc.",

  "identifier": "403639508"

  },

  {

  "display_name": "Pinterest",

  "developer_name": "Pinterest, Inc.",

  "identifier": "429047995"

  },

  {

  "display_name": "Photos",

  "developer_name": "Apple, Inc.",

  "identifier": ""

  },

  {

  "display_name": "Podcasts",

  "developer_name": "Apple, Inc.",

  "identifier": "525463029"

  },

  {

  "display_name": "Remote",

  "developer_name": "Apple, Inc.",

  "identifier": "284417350"

  },

  {

  "display_name": "Safari",

  "developer_name": "Apple, Inc.",

  "identifier": ""

  },

  {

  "display_name": "Skype",

  "developer_name": "Skype Communications S.a.r.l",

  "identifier": "304878510"

  },

  {

  "display_name": "Slack",

  "developer_name": "Tiny Speck, Inc.",

  "identifier": "618783545"

  },

  {

  "display_name": "Tumblr",

  "developer_name": "Tumblr, Inc.",

  "identifier": "305343404"

  },

  {

  "display_name": "Twitter",

  "developer_name": "Twitter, Inc.",

  "identifier": "333903271"

  },

  {

  "display_name": "Vesper",

  "developer_name": "Q Branch",

  "identifier": "655895325"

  },

  {

  "display_name": "Videos",

  "developer_name": "Apple, Inc.",

  "identifier": ""

  },

  {

  "display_name": "Vine",

  "developer_name": "Vine Labs, Inc.",

  "identifier": "592447445"

  },

  {

  "display_name": "WhatsApp",

  "developer_name": "WhatsApp, Inc.",

  "identifier": "310633997"

  },

  {

  "display_name": "WWDC",

  "developer_name": "Apple, Inc.",

  "identifier": "640199958"

  },

]


Application.h:

#import <Foundation/Foundation.h>



@interface Application : NSObject

@property (nonatomic, strong) NSString *displayName;

@property (nonatomic, strong) NSString *developerName;

@property (nonatomic, strong) NSString *identifier;



- (instancetype)initWithDictionary:(NSDictionary *)dict;

+ (NSArray *)applicationsFromJSONAtPath:(NSString *)path;

+ (NSArray *)applicationsFromJSON:(id)JSON;

@end


Application.m:

#import "Application.h"



@implementation Application



- (instancetype)initWithDictionary:(NSDictionary *)dict

{

    self = [super init];

    if (self) {

        self.displayName = dict[@"display_name"];

        self.developerName = dict[@"developer_name"];

        self.identifier = dict[@"identifier"];

    }

    return self;

}



+ (NSArray *)applicationsFromJSONAtPath:(NSString *)path

{

    NSData *data = [NSData dataWithContentsOfFile:path];

    NSArray *JSON = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions|NSJSONWritingPrettyPrinted error:nil] mutableCopy];

    

    return [self applicationsFromJSON:JSON];

}



+ (NSArray *)applicationsFromJSON:(id)JSON

{

    NSMutableArray *objects = [NSMutableArray new];

    

    for (NSDictionary *dictionary in JSON) {

        Application *obj = [[Application alloc] initWithDictionary:dictionary];

        [objects addObject:obj];

    }

    

    return objects;

}

@end


JsonToArrayViewController.m:

#import "JsonToArrayViewController.h"

#import "Application.h"



static NSString *identifier = @"Cell";

@interface JsonToArrayViewController ()

@property(nonatomic,strong)NSArray* arrayData;

@end



@implementation JsonToArrayViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}



-(NSArray*)arrayData

{

    if (!_arrayData) {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"applications" ofType:@"json"];

        _arrayData = [Application applicationsFromJSONAtPath:path];

    }

    return _arrayData;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arrayData.count;

}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        cell.backgroundColor = [UIColor whiteColor];

        cell.selectionStyle = UITableViewCellSelectionStyleGray;

    }

    Application *app = [[self arrayData] objectAtIndex:indexPath.row];

    cell.textLabel.text = app.displayName;

    return cell;

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}



@end


效果图如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值