JSON&XML解析

JSON

使用JSONKit  需要添加-fno-objc-arc  是mrc的 修改之后 进行isa 替换 就能编译通过

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = [NSString stringWithFormat:@"/Users/wyc/Desktop/jsontext.date"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSURL *url = [NSURL URLWithString:@"file:///Users/wyc/Desktop/jsontext.date"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
       id result =  [[JSONDecoder decoder]objectWithData:data];
        NSDictionary *dic = [result objectForKey:@"people"];
        for (id item in dic) {
            NSLog(@"%@",item[@"email"]);
            NSLog(@"%@",item[@"firstName"]);
            NSLog(@"%@",item[@"lastName"]);
        }
    }];
}

  

XML

 
@interface Book : NSObject
@property (nonatomic, assign) NSInteger idd;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *author;
@property (nonatomic, copy) NSString *summary;
@end

#import "Book.h"
@interface ViewController ()<NSXMLParserDelegate>
//目标数组
@property (nonatomic, strong) NSMutableArray *Books;
//当前正在解析的对象
@property (nonatomic, strong) Book *currentBook;
//拼接字符串
@property (nonatomic, strong) NSMutableString *elementString;
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadXML];
}
-(void)loadXML
{
    NSURL *url = [NSURL URLWithString:@"file:///Users/wyc/Desktop/aaa.xml"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        //实例化xml解析器
        NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
        //设置代理
        parser.delegate = self;
        //开始解析  一旦开始解析 后续工作都是由代理方法实现
        [parser parse];
    }];
}
//mark:delegatexml
//打开文档
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    //清除数组
    [self.roots removeAllObjects];
}
//开始节点 只能读取出节点属性名称跟值
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
//    NSLog(@"elementName:%@",elementName);
//    NSLog(@"namespaceURI:%@",namespaceURI);
//    NSLog(@"qName:%@",qName);
//    NSLog(@"attributeDict:%@",attributeDict);
    if ([elementName isEqualToString:@"Book"]) {
        self.currentBook = [[Book alloc]init];
        self.currentBook.idd = @([attributeDict[@"idd"] intValue]);
//        NSLog(@"attributeDict:%@",attributeDict[@"idd"]);
    }
    [self.elementString setString:@""];
}
//发现节点内容
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [self.elementString appendString:string];
//    NSLog(@"内容为:%@",string);
}
//结束节点
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//    NSLog(@"%@",elementName);
    if ([elementName isEqualToString:@"Book"]) {
        [self.Books addObject:self.currentBook];
    }else if (![elementName isEqualToString:@"Books"]){
        [self.currentBook setValue:self.elementString forKey:elementName];
    }
//    }else if ([elementName isEqualToString:@"title"]){
//        self.currentBook.title = self.elementString;
//    }else if ([elementName isEqualToString:@"author"]){
//        self.currentBook.author = self.elementString;
//    }else if ([elementName isEqualToString:@"summary"]){
//        self.currentBook.summary = self.elementString;
//    }
}
//结束文档
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"文档解析结束:%@",self.Books);
    
}
//lazy
-(NSMutableArray *)roots
{
    if (_Books == nil) {
        _Books = [[NSMutableArray alloc]init];
    }
    return _Books;
}
-(NSMutableString *)elementString
{
    if (_elementString == nil) {
        _elementString = [[NSMutableString alloc]init];
    }
    return _elementString;
}

  

posted @ 2015-12-10 22:53  若云  阅读(260)  评论(0编辑  收藏  举报