@property (nonatomic, strong) NSMutableArray *JSONArray;
懒加载
- (NSMutableArray *)JSONArray
{
if (_JSONArray == nil) {
self.JSONArray = [NSMutableArray array];
}
return _JSONArray;
}
- (IBAction)JSONKitButtonDidClicked:(UIButton *)sender {
// 1、拿到 json 文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"json"];
// 2、根据路径获取文件内容为 NSData 对象
NSData *data = [NSData dataWithContentsOfFile:path];
// 3、解析为数组
NSArray *array = [data objectFromJSONData];
for (NSDictionary *dict in array) {
StudentModal *modal = [StudentModal new];
[modal setValuesForKeysWithDictionary:dict];
[self.JSONArray addObject:modal];
}
for (StudentModal *modal in self.JSONArray) {
NSLog(@"JSONArray = %@, %ld, %@", modal.name, modal.number, modal.hobby);
}
[self.JSONArray removeAllObjects];
// 把OC里面的数据类型转化成 json 串
NSString *yadong;
[yadong JSONString];
NSArray *testArray = @[@"wewe", @"www", @"qq"];
NSString *result = [testArray JSONString];
NSLog(@"result = %@", result);
}
版权声明:本文为outlan原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/yadong_zhao/article/details/46971487