第一种方式:使用老师的方法 快被坑死了 不过也意外的学到了一些别的方法
步骤一:新建工程 把SBJson的框架内容拉进工程
步骤二:新建或拉进工程一个资源文件 例如:
json.txt 内容如下:
{"data":[{"id":9,"name":"华杉科技","channeltype":0},{"id":10,"name":"\u6b4c\u534e\u6709\u7ebf","channeltype":0},{"id":11,"name":"\u767e\u5e74\u8001\u6821","channeltype":0},{"id":17,"name":"\u76f4\u64ad","channeltype":2},{"id":24,"name":"\u5e08\u8005\u5370\u8c61","channeltype":0}]}
步骤三:新建model模型 一般情况下一个字典对应一个对象
编辑Model.h如下:
//
// Model.h
// json解析
//
// Created by apple on 15/9/18.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *channeltype;
-(void) print;
@end
编辑Model.m如下:
//
// Model.m
// json解析
//
// Created by apple on 15/9/18.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "Model.h"
@implementation Model
-(void) print
{
NSLog(@"%@ %@ %@",_id,_name,_channeltype);
}
@end
步骤四:新建转换类
//
// AssignToObject.h
// json解析
//
// Created by apple on 15/9/18.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface AssignToObject : NSObject
// 这个方法可以直接把从json中得到的数组中的字典转化为具体的对象,其里面都是封装好的具体对象
+(NSMutableArray *) customModel:(NSString *) modelClass ToArray:(id) array;
@end
编辑其.m文件:
//
// AssignToObject.m
// json解析
//
// Created by apple on 15/9/18.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "AssignToObject.h"
@implementation AssignToObject
+(NSMutableArray *) customModel:(NSString *) modelClass ToArray:(id) array
{
//用来存放一个类中有几个成员变量
unsigned int outCount = 0;
NSLog(@" ----------");
//以下方法会把指定的类的所有属性存放在一个指针数组中并返回,同时把指定类的属性个数存放再一个短整型变量中以访问指针变量赋值的方式
objc_property_t *properties = class_copyPropertyList(NSClassFromString(modelClass), &outCount);
// 创建一个可变数组用来存放每一个model对象
NSMutableArray *objectArr = [NSMutableArray array];
for (int i = 0 ; i<[array count]; i++) {
// 取出字典数组中的一个对象
id jsonDic = [array objectAtIndex:i];
// 若数组中的对象不是字典对象就跳过它。继续下一个
if (![jsonDic isKindOfClass:[NSDictionary class]]) {
continue;
}
//根据类名创建相应的对象
id model = [[NSClassFromString(modelClass) alloc] init];
// 判断类中的每一个属性
for (int j=0; j<outCount; j++) {
// 获取类中第j个成员变量信息
objc_property_t property = properties[j];
// 获取类中的第j个成员变量将其转化为字符串
NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
// 获取json串中的键对应的值
id value = [jsonDic objectForKey:propertyName];
// 判断上面取得的值是否存在,不存在就去转换下一个属性
// 值为空或如果Value是一个不存在的对象的话
if (!value||[value isKindOfClass:[NSNull class]]) {
continue;
}
[model setValue:value forKey:propertyName];
}
// 把转化后的对象加到一个数组中
[objectArr addObject:model];
}
return objectArr;
}
@end
NSString *path = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"];
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; // 将文件的内容读取到一个字符串中
NSDictionary *dict = [str JSONValue]; // 取出的内容转化为一个整体字典
NSArray *arr = [dict objectForKey:@"data"]; // 从字典中取出一个数组
NSMutableArray *objectArr = [AssignToObject customModel:@"Model" ToArray:arr];
for(Model *object in objectArr)
{
[object print];
}
============================================================
妈的快被王泽这华杉科技的逼货生的坑死了 老子直接用KVC 简单省事
另外发现直接读出的内容会自动转换相对应的数据类型
新建工程 新建json.txt资源文件 编辑如下:
{"data":[{"id":9,"name":"华杉科技","channeltype":0,"date":"2012-3-15"},
{"id":10,"name":"\u6b4c\u534e\u6709\u7ebf","channeltype":0,"date":"2012-3-15"},
{"id":11,"name":"\u767e\u5e74\u8001\u6821","channeltype":0,"date":"2012-3-15"},
{"id":17,"name":"\u76f4\u64ad","channeltype":2,"date":"2012-3-15"},
{"id":24,"name":"\u5e08\u8005\u5370\u8c61","channeltype":0,"date":"2012-3-15"}]}
新建模型如下:
//
// Model.h
// SBJsonDemo
//
// Created by wangze on 15/9/18.
// Copyright (c) 2015年 wangze. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, assign) int id;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int channeltype;
@property (nonatomic, strong) NSDate *date;
-(void)print;
-(instancetype) initWithDict:(NSDictionary *) dict;
+(instancetype) modelWithDict:(NSDictionary *) dict;
@end
编辑Model.m如下:
//
// Model.m
// SBJsonDemo
//
// Created by wangze on 15/9/18.
// Copyright (c) 2015年 wangze. All rights reserved.
//
#import "Model.h"
@implementation Model
-(void)print
{
NSLog(@"%d %@, %d %@", _id, _name, _channeltype,_date);
}
-(instancetype) initWithDict:(NSDictionary *) dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype) modelWithDict:(NSDictionary *) dict
{
return [[self alloc] initWithDict:dict];
}
@end
新建转化类如下:
//
// AssignToObject.h
// GainProperty
//
// Created by wangze on 13-7-18.
// Copyright (c) 2013年 wangze. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface AssignToObject : NSObject
//这个方法可以直接把从json中得到的数组中的字典转化为具体的对象,其里面都是封装好的具体对象。
+ (NSMutableArray *)customModel:(NSString *)modelClass ToArray:(id)array;
@end
编辑其.m文件如下:
//
// AssignToObject.m
// GainProperty
//
// Created by wangze on 13-7-18.
// Copyright (c) 2013年 wangze. All rights reserved.
//
#import "AssignToObject.h"
#import "Model.h"
@implementation AssignToObject
+ (NSMutableArray *)customModel:(NSString *)modelClass ToArray:(id)jsonArray
{
//创建一个数组用来存放对象;
NSMutableArray *objectArr = [[NSMutableArray alloc] initWithCapacity:1];
for (int i = 0; i < [jsonArray count]; i++)
{
//取出数组中的一个对象
id jsonDic = [jsonArray objectAtIndex:i];
//若数组中的对象不是字典对象就跳过它。继续下一个。
if(![jsonDic isKindOfClass:[NSDictionary class]])
{
continue;
}
//创建一个传过来字符串(类名)相应的对象
id model = [[NSClassFromString(modelClass) alloc] initWithDict:jsonDic];
//把转化完成的对象加到一个数组中。
[objectArr addObject:model];
}
return objectArr;
}
@end
调用如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"];
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSDictionary *dic = [str JSONValue];
NSArray *arr = [dic objectForKey:@"data"];
NSMutableArray *objectArr = [AssignToObject customModel:@"Model" ToArray:arr];
for (Model *object in objectArr)
{
[object print];
}
运行结果如下: