IOS JSON数据解析

//获取文件中的json数据
//JsonData 是文件名
//json 是文件后缀民
NSString *path=[[NSBundle mainBundle] pathForResource:@"JsonData" ofType:@"json"];
NSData *data=[[NSData alloc] initWithContentsOfFile:path];
//解析成字典
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

解析成对象放到数组中

if ([dic isKindOfClass:[NSDictionary class]]) {
    //开始解析数据
    _tableArray=[[NSMutableArray alloc]init];
    NSArray *programmersArray=[dic objectForKey:@"programmers"];
    //遍历数组对象
    for (int i=0; i

Author 对象头文件

@interface Author : NSObject
@property (nonatomic,copy) NSString *firstName;
@property (nonatomic,copy) NSString *lastName;
@property (nonatomic,copy) NSString *email;
-(NSString *) getName;
@end

Author .m文件

@implementation Author
-(NSString *)getName{
return [NSString stringWithFormat:@"%@ %@",_lastName,_firstName];
}
@end

模版son
{"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
}

你可能感兴趣的:(IOS JSON数据解析)