在项目中,因为需求的更迭,一个列表里的数据会不断的改变,有人会选择直接在控制器里用代码写死数组,需求更迭的时候,去控制器里增删改数组元素,有人会选择用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;
@endApplication.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;
}
@endJsonToArrayViewController.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效果图如下:

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

367

被折叠的 条评论
为什么被折叠?



