Monday, 20 June 2016

json Parsing Using JSONModel

json Parsing Using JSONModel


*json GET Method & Files Already Given in Previous Demo ....

Here Below JSONModel Folder Given Below Which Have All File Of Json Post ....


Click Here To Download :-   Download


This Folder Must Be Import in Your Bundle ....


Coding Part .....




MainModel.h


#import <Foundation/Foundation.h>
#import "JSONModel.h"
#import "DataModel.h"

@interface MainModel : JSONModel

@property (strong , nonatomic) NSArray < DataModel , ConvertOnDemand > *categories ;

@end



MainModel.m



#import "MainModel.h"

@implementation MainModel

@end



DataModel.h


#import <Foundation/Foundation.h>
#import "JSONModel.h"

@protocol DataModel <NSObject>

@end

@interface DataModel : JSONModel

@property (strong , nonatomic) NSString *category_name ;

@end



DataModel.m


#import "DataModel.h"

@implementation DataModel

@end





* ViewController ( Where You Wnat To use ) 

ViewController.h :




#import <UIKit/UIKit.h>

#import "MainModel.h"
#import "DataModel.h"

@interface ViewController : UIViewController

@property (strong , nonatomic) IBOutlet UITableView *tblObj ;

//-------- MainModel Obj -------//

@property (strong , nonatomic) MainModel *obj ;


@property (nonatomic ,strong) DataModel *dataModelObj ;

@end



ViewController.m :



#import "ViewController.h"
#import "NSURLConnection_Class.h"

@interface ViewController () < NSURLConnection_CustomDelegate>

@end

@implementation ViewController
@synthesize obj, dataModelObj, tblObj ;

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self getHomeData];
    
}


#pragma mark - Get Data from Json

-(void)getHomeData
{
    //---------------------- Request for Getting School List  ----------------------//
    

    NSURLConnection_Class *objURL = [[NSURLConnection_Class alloc] init] ;
    [objURL RequestForURL:@"Your URL"];
    objURL.delegate = self ;
    
}

#pragma mark - Custom Delegate [ NSURLConnection ]

-(void)connectionFailWithError:(NSError *)error
{
    NSLog(@"Error: %@",error.localizedDescription) ;
}

-(void)connectionFinishWithResponse:(NSString *)responseString
{
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] ;
    
    NSLog(@"%@",responseDict);
    
    obj = [[MainModel alloc] initWithDictionary:responseDict error:nil] ;
    
    for (int i = 0; i < obj.categories.count; i ++)
    {
        dataModelObj = obj.categories[i] ;
    }
    
    [tblObj reloadData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return obj.categories.count ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tblObj dequeueReusableCellWithIdentifier:@"cell"];
    
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] ;
    }
    
    dataModelObj = obj.categories[indexPath.row] ;
    
    cell.textLabel.text = dataModelObj.category_name ;
    
    return cell ;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


No comments:

Post a Comment