Aftab Ali
Aftab Ali

Reputation: 107

how to fetch json data and store it in an array in iphone app and access it

I am using this code to access the json data. It shows the two objects in array but then stops

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://celeritas-solutions.com/emrapp/AppointmentListings.php"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
//appDelegate.books = [[NSMutableArray alloc] initWithCapacity:0];

appDelegate.books = [[NSMutableArray alloc] init];

NSArray *results = [parser objectWithString:json_string error:nil];
NSLog(@"%@", results);

for (int i=0; i<[results count]; i++) {
    AppointmentListing *aBook = [[AppointmentListing alloc] initWithDictionary:[results objectAtIndex:i]];
    [appDelegate.books addObject:aBook];
    [aBook release];    
}

Below is my json data

[{"ProviderNPI":"7610922880","PatientID":"556712","AppointmentDate":"2012-03-20","AppointmentTime":"09:00:00","AppointmentListingsID":"1","UpdateDateTime":"2012-03-20 00:30:35"},{"ProviderNPI":"7610922880","PatientID":"712211","AppointmentDate":"2012-03-20","AppointmentTime":"10:00:00","AppointmentListingsID":"2","UpdateDateTime":"2012-03-20 00:31:25"}]

Upvotes: 1

Views: 1508

Answers (4)

Kuldeep
Kuldeep

Reputation: 2589

Just copy & paste & replace to your code will solve your problem :

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://celeritas-solutions.com/emrapp/AppointmentListings.php"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *books=[json_string JSONValue];

for (int i=0; i<[books count]; i++) {
    /*AppointmentListing  *aBook = [[AppointmentListing alloc] initWithDictionary:[books objectAtIndex:i]];
    [books addObject:aBook];
    [aBook release];*/

    NSDictionary *dict=[books objectAtIndex:i];

    book.appointmentdate=[dict valueForKey:@"AppointmentDate"];
    book.AppointmentListingsID=[dict valueForKey:@"AppointmentListingsID"];
    book.AppointmentTime=[dict valueForKey:@"AppointmentTime"];
    book.PatientID=[dict valueForKey:@"PatientID"];
    book.ProviderNPI=[dict valueForKey:@"ProviderNPI"];
    book.UpdateDateTime=[dict valueForKey:@"UpdateDateTime"];

    NSLog([dict description],nil);
}

NSLog(@"%@", json_string);


//May this will help you out.
//You just need to import "Json.h" to your class header


//Log I got from this piece of code :
2012-03-21 14:34:34.841 ProgressiveDownload[1045:f803] {
    AppointmentDate = "2012-03-20";
    AppointmentListingsID = 1;
    AppointmentTime = "09:00:00";
    PatientID = 556712;
    ProviderNPI = 7610922880;
    UpdateDateTime = "2012-03-20 00:30:35";
}

2012-03-21 14:34:37.356 ProgressiveDownload[1045:f803] {
    AppointmentDate = "2012-03-20";
    AppointmentListingsID = 2;
    AppointmentTime = "10:00:00";
    PatientID = 712211;
    ProviderNPI = 7610922880;
   UpdateDateTime = "2012-03-20 00:31:25";
}

Upvotes: 2

Khalid Usman
Khalid Usman

Reputation: 1282

//This is for the Dictionary You can use in the same way for Array.

NSString *url = @"http://weather.yahooapis.com/forecastjson?w=22117";
NSString *myJson=[[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:url]];

if([myJson length]==0)
{
    [myJson release];
    return;
}
SBJsonParser *praser=[[SBJsonParser alloc]init];
    NSDictionary *dicParser=[[praser objectWithString:myJson error:nil] copy];

Upvotes: 0

Rama Rao
Rama Rao

Reputation: 1033

NSMutableData *responseData1= [NSMutableData data] ;
responseData1 = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString:@"http://celeritas-solutions.com/emrapp/AppointmentListings.php"]];
// NSLog(@"%@",responseData1);
NSString *responseString1 = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding];
//NSLog(@"REs:-->%@",responseString1);
//[responseData1 release];
responseData1 = nil;
NSMutableArray *responseArray = [[NSMutableArray alloc]init];
responseArray = (NSMutableArray *)[responseString1 JSONValue];
// NSLog(@"ghfchfvghv%@",responseArray);
//[responseString1 release];
//return responseArray;
NSLog(@"%@",[responseArray description]);

Upvotes: 0

Kanan Vora
Kanan Vora

Reputation: 2122

From your code I presume that you are using that JSON folder for json parsing, containing many Json Parser related file...

Do one thing, First import JSON.h file:

#import "JSON.h"

Then parse your response string like this,

first create an object like this:

SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *response_dict = [json objectWithString:json_string];

And you're done!!!

Upvotes: 0

Related Questions