Showing posts with label JSON related.... Show all posts
Showing posts with label JSON related.... Show all posts

23 Mar 2017

Converting JSON into an Objective-C object



Converting JSON into an Objective-C object

// Retrieve local JSON file called example.json

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"];

 // Load the file into an NSData object called JSONData 

NSError *error = nil;

NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];

 // Create an Objective-C object from JSON Data

id JSONObject = [NSJSONSerialization
                         JSONObjectWithData:JSONData
                         options:NSJSONReadingAllowFragments
                         error:&error];

2 Mar 2017

NSJSONSerialization Code to get Data Code..


NSJSONSerialization Code to get Data Code..





#import "ViewController.h"
@interface ViewController ()
{
    NSMutableArray * arr,*priceArr1;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error;
    arr = [[NSMutableArray alloc]init];
    priceArr1 = [[NSMutableArray alloc]init];
    
   self.jsonDix = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat: @"https://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"]]] options:kNilOptions error:&error];
    
    for (int i=0; i<9; i++)
    {
        
        NSLog(@"Parsed json data is ::::: %@ \n", [[[[[_jsonDix objectForKey:@"feed" ]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"label"]);
        
        [arr addObject: [[[[[_jsonDix objectForKey:@"feed" ]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"label"]];
        //NSLog(@"array elements are === %@",[arr objectAtIndex:i]);
        
        [priceArr1 addObject: [[[[[_jsonDix objectForKey:@"feed" ]objectForKey:@"entry"]objectAtIndex:i]objectForKey:@"im:price"]objectForKey:@"label"]];
       NSLog(@"array elements are === %@",[priceArr1 objectAtIndex:i]);
    }
    

}

2 Jan 2017

JSON POST DATA


 JSON POSTDATA...


@property NSMutableURLRequest *loginURLRequest;
@property NSURLSessionConfiguration *loginURLSessionConfg;
@property NSURLSession *loginURLSession;
@property NSURLSessionDataTask *loginDataTask;


- (IBAction)loginTapped:(id)sender {


    self.loginURLRequest=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://www.brninfotech.com/pulse/modules/admin/ValidateLogin.php"]];

    [self.loginURLRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    self.loginURLRequest.HTTPMethod=@"POST";

    NSString * post=[[NSString alloc] initWithFormat:@"registeredEmail=%@&registeredPassword=%@&funcName=%@",self.email.text,self.password.text,@"verifyLogin"];
    NSData * postData=[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString * postLength=[NSString stringWithFormat:@" %lu ",(unsigned long)[postData length]];
    
    [self.loginURLRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [self.loginURLRequest setHTTPBody:postData];
    
    self.loginURLSessionConfg=[NSURLSessionConfiguration defaultSessionConfiguration];
    self.loginURLSession=[NSURLSession sessionWithConfiguration:self.loginURLSessionConfg];
    
    self.loginDataTask=[self.loginURLSession dataTaskWithRequest:self.loginURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
    [AppDelegate SharedAppDelegateImplementation].profileDictionary=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"got response from server %@",[AppDelegate SharedAppDelegateImplementation].profileDictionary);

    [self.loginDataTask resume];   

}

JSON GET DATA


 JSON GetData...

@property NSMutableURLRequest *urlRequestFree;
@property NSURLSessionConfiguration *urlSessionConfigurationFree;
@property NSURLSession *urlSessionFree;
@property NSURLSessionDataTask *dataTaskFree;
@property NSMutableDictionary *freeAppsData;


-(void) JsonPost  {

self.urlRequestFree=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://itunes.apple.com/in/rss/topfreeapplications/limit=200/json"]];
    self.urlSessionConfigurationFree=[NSURLSessionConfiguration defaultSessionConfiguration];
    self.urlSessionFree=[NSURLSession sessionWithConfiguration:self.urlSessionConfigurationFree];
    
    self.dataTaskFree=[self.urlSessionFree dataTaskWithRequest:self.urlRequestFree completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        self.freeAppsData=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@",self.freeAppsData);
    }];
    [self.dataTaskFree resume];

    
}

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0