- (NSArray *)lianxis
{
    if (_lianxis == nil) {
       
        //JSON文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"lianxis.json" ofType:nil];
        
        //加载JSON文件
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        //将JSON数据转为NSArray或NSDictionary
        NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        //将字典转为数组
        NSMutableArray *lianxArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            LianxiModel *model = [LianxiModel lianxiWithDict:dict];
            [lianxArray addObject:model];
        }
        
        //赋值
        _lianxis = lianxArray;
    }
    return _lianxis;
}

模型.h

#import <Foundation/Foundation.h>

@interface LianxiModel : NSObject
/**
 *  练习按钮上的文字
 */
@property(nonatomic,copy)NSString *title;
/**
 *  练习按钮上的图标
 */
@property(nonatomic,copy)NSString *icon;

- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)lianxiWithDict:(NSDictionary *)dict;
@end

模型.m

#import "LianxiModel.h"

@implementation LianxiModel

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        self.icon = dict[@"icon"];
        self.title = dict[@"title"];
    }
    return  self;
}
+ (instancetype)lianxiWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
@end


GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐