如何获取json数据并将其存储在iphone应用程序中的数组中并访问它

时间:2012-03-21 05:14:55

标签: iphone objective-c

我正在使用此代码来访问json数据。它在数组中显示两个对象,但随后停止

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];    
}

以下是我的json数据

[{"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"}]

4 个答案:

答案 0 :(得分:2)

只需复制&amp;粘贴&amp;替换为您的代码将解决您的问题:

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";
}

答案 1 :(得分:0)

从您的代码中我假设您正在使用该JSON文件夹进行json解析,其中包含许多与Json Parser相关的文件......

做一件事,首先导入JSON.h文件:

#import "JSON.h"

然后解析你的响应字符串,

首先创建一个这样的对象:

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

你已经完成了!!!

答案 2 :(得分:0)

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]);

答案 3 :(得分:0)

//这适用于字典您可以以相同的方式使用数组。

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];