AFNetworking解析 json 和 xml 数据

先导入 AFNetworking 第三方
创建我们所需的文件

AFNetworking解析 json 和 xml 数据_第1张图片
屏幕快照 2017-09-17 下午7.36.58.png

Hero.h

#import 

@interface Hero : NSObject

@property(nonatomic,strong)NSString *name,*like;

@end

LoadData.h

#import "Hero.h"

@interface LoadData : NSObject
{
    NSMutableArray *_array;
    NSString *newElement;
    Hero *he;
    
}

+(instancetype)shareLoadData;
-(void)getName;


LoadData.m

#import "LoadData.h"
#import "AFNetworking.h"
#import "Hero.h"

#define TEXT_URL @"http:127.0.0.1/1508E.xml"
static LoadData *ld = nil;



@implementation LoadData



+(instancetype)shareLoadData
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ld = [[LoadData alloc] init];
    });
    
    return ld;
}






-(void)getName
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [[AFXMLParserResponseSerializer alloc] init];
    
    [manager GET:TEXT_URL parameters:0 success:^(NSURLSessionDataTask *task, id responseObject)
    {
        NSXMLParser *parser = (NSXMLParser *)responseObject;
        parser.delegate = self;
        
        
        [parser parse];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    _array = [[NSMutableArray alloc] init];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    //NSLog(@"=======%@",_array);
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"123" object:_array];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"hero"])
    {
        he = [[Hero alloc] init];
        [_array addObject:he];
        
    }
    newElement = elementName;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName
{
    newElement = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([newElement isEqualToString:@"name"])
    {
        he.name = string;
    }
    else if ([newElement isEqualToString:@"like"])
    {
        he.like = string;
    }
}

@end

ViewController.m文件

#import "ViewController.h"
#import "LoadData.h"
#import "SecondViewController.h"

@interface ViewController ()
{
    NSMutableArray *theArray;
    
    UITableView *table;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(click:) name:@"123" object:nil];
    [[LoadData shareLoadData] getName];
    
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    table.delegate = self;
    table.dataSource = self;
    
    [self.view addSubview:table];
    
    
}
-(void)click:(NSNotification *)notifi
{
    theArray = notifi.object;
    //NSLog(@"=======%@",theArray);
    
    [table reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return theArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
    }
    cell.textLabel.text = [[theArray objectAtIndex:indexPath.row] name];
    cell.detailTextLabel.text = [[theArray objectAtIndex:indexPath.row] like];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *theSec = [[SecondViewController alloc] init];
    [self presentViewController:theSec animated:YES completion:nil];
}

@end

SecondViewController.m中

#import "SecondViewController.h"
#import "AFNetworking.h"
#define TEXT_JSON @"http://127.0.0.1/1508E.json"
@interface SecondViewController ()
{
    UITableView *table;
    NSDictionary *dic;
}
@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
        
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [[AFJSONResponseSerializer alloc] init];
    
    [manager GET:TEXT_JSON parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
    {
        dic = (NSDictionary *)responseObject;
        NSLog(@"===========%@",dic);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    
    table.delegate = self;
    table.dataSource = self;
    
    [self.view addSubview:table];
   
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dic.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [dic.allKeys objectAtIndex:section];
    
    return [dic[key]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
    }
    NSString *key = [dic.allKeys objectAtIndex:indexPath.section];
    
    cell.textLabel.text = [[dic[key]objectAtIndex:indexPath.row] objectForKey:@"name"];
    cell.detailTextLabel.text = [[dic[key]objectAtIndex:indexPath.row] objectForKey:@"like"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

你可能感兴趣的:(AFNetworking解析 json 和 xml 数据)