json解析
#import
#import "Model.h"
#import "JSON.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *path = @"/Users/qianfeng/Desktop/OC课堂老师代码/oc上课代码/1231/easy.json";
// 系统自带方法的解析
NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *array = dict[@"result"];
// 数据转成数据model
NSMutableArray *arrayM = [NSMutableArray array];
for (int i = 0; i < array.count; i++) {
NSDictionary *dic = array[i];
Model *model = [[Model alloc] initWithDict:dic];
[arrayM addObject:model];
}
NSLog(@"%@",arrayM);
/* // 使用第三方的SBjson
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSDictionary *dictT = [jsonParser objectWithString:str];
*/
}
return 0;
}
#import
@interface Model : NSObject
@property (nonatomic,copy)NSString * name;
@property (nonatomic,copy)NSString * address;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
#import "Model.h"
@implementation Model
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.name = dict[@"name"];
self.address = dict[@"address"];
}
return self;
}
@end
json解析easy.json内容
{
"New name-value pair 0" : {},
"result" : [
{
"addrss" : "中国北京",
"name" : "龙虾"
},
{
"addrss" : "韩国首尔",
"name" : "螃蟹"
},
{
"addrss" : "美国华盛顿",
"name" : "蛤喇"
},
{
"addrss" : "英国伦敦",
"name" : "苹果"
},
{
"addrss" : "中国台湾",
"name" : "香蕉"
},
{
"addrss" : "砀山",
"name" : "大酥梨"
}
]
}
XML解析
#import
#import "QFParser.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
QFParser *qfParser = [[QFParser alloc] init];
[qfParser test];
}
return 0;
}
#import
@interface Student : NSObject
@property (nonatomic,copy)NSString * name;
@property (nonatomic,copy)NSString * address;
@end
#import "Student.h"
@implementation Student
- (NSString *)description{
return [[NSString alloc] initWithFormat:@"%@,%@",_name,_address ];
}
@end
#import
@interface QFParser : NSObject
- (void)test;
@end
#import "QFParser.h"
#import "Student.h"
@interface QFParser ()
@property (nonatomic, copy)NSMutableString *elementStr;
@property (nonatomic, strong)NSMutableArray *arrayM;
@property (nonatomic, strong)Student *student;
@end
@implementation QFParser
- (void)test
{
// 1. 数据接口
NSString *path = @"/Users/qianfeng/Desktop/OC课堂老师代码/oc上课代码/1231/test.xml";
NSData *data = [NSData dataWithContentsOfFile:path];
// XML 解析器
NSXMLParser *paser = [[NSXMLParser alloc] initWithData:data];
paser.delegate = self;
// 启动解析
[paser parse];
}
#pragma mark -NSXMLParser Delegate方法
#pragma mark 1.开始解析文档,在这里做初始化工作
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
_arrayM = [NSMutableArray array];
_elementStr = [NSMutableString string];
}
#pragma mark 开始解析节点
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// elementName 指的是 结束节点的名称
// NSLog(@"----%@",elementName);
if ([elementName isEqualToString:@"student"]) {
_student = [[Student alloc] init];
}
// NSLog(@"$$$$$$$$$%@",_elementStr);
// 清空拼接的字符串
[_elementStr setString:@""];
}
#pragma mark 循环查找节点内容
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// NSLog(@"######%@",string);
[_elementStr appendString:string];
}
#pragma mark 一个节点完成了会调这个方法
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// NSLog(@"&&&&&&&&%@",elementName);
NSString *result = [NSString stringWithString:_elementStr];
if ([elementName isEqualToString:@"student"]) {
// NSLog(@"%@",_student);
[_arrayM addObject:_student];
// 说明一个student节点读取完成
}else if ([elementName isEqualToString:@"name"]){
NSLog(@"name%@",result);
_student.name = result;
// 说明一个name节点读取完成
}else if ([elementName isEqualToString:@"address"]){
_student.address = result;
NSLog(@"address%@",result);
// 说明一个address节点读取完成
}
}
#pragma mark 解析完成,处理数据
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
for (Student *student in _arrayM) {
NSLog(@"\n\n%@\n",student);
}
}
#pragma mark 解析出错,清理数据,只能判断格式有没有错误,不能判断内容是否错误 也就是说 只有符合条件的才能遍历出来,不符合的不会遍历出来。
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
[_arrayM removeAllObjects];
NSLog(@"%@",parseError);
}
@end
test.xml 内容
优衣库
北京三里屯
黑人
美国加利福尼亚
毛爷爷
中国精英
葫芦小金刚
葫芦娃
蛇精
报告大王