如何在iphone应用程序中获取json数据并将其存储在数组中并访问它
我使用此代码访问json数据。它显示数组中的两个对象,但随后停止如何在iphone应用程序中获取json数据并将其存储在数组中并访问它,iphone,objective-c,Iphone,Objective C,我使用此代码访问json数据。它显示数组中的两个对象,但随后停止 SBJsonParser *parser = [[SBJsonParser alloc] init]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://celeritas-solutions.com/emrapp/AppointmentListings.php"]]; NSData *response = [NS
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文件夹进行JSON解析,其中包含许多与JSON解析器相关的文件 做一件事,首先导入JSON.h文件:
#import "JSON.h"
然后像这样解析您的响应字符串
首先创建一个如下所示的对象:
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *response_dict = [json objectWithString:json_string];
你完了 只需复制、粘贴和替换代码即可解决您的问题:
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";
}
NSURLRequest*request=[nsurlRequestWithURL:[nsurlUrlWithString:@]http://celeritas-solutions.com/emrapp/AppointmentListings.php"]];
NSData*响应=[NSURLConnection sendSynchronousRequest:request returningResponse:nil错误:nil];
NSString*json_string=[[NSString alloc]initWithData:响应编码:NSUTF8StringEncoding];
NSArray*books=[json_string JSONValue];
for(int i=0;i//这是一个字典,您可以用与数组相同的方式使用它
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];
NSString*responseString=[request responseString];
NSMutableDictionary*responseJSON=[responseString JSONValue];
。然后您可以简单地使用NSDictionary并显示其中的数据。@ParthBhatt如何做我也给出了上面的代码参考此链接:@Aftab Ali您只需复制/粘贴我在该页面中发布的代码就可以了。是的,我正在导入JSON,但在下面我只添加了这个,我将如何添加到数组中,因为我已经添加了donebut ap当我使用这个时,p崩溃了看看上面的代码,我下载了数据日志。是的,现在它正在工作,但是如何将它们添加到像book.AppointDate这样的数组中;像这样,我已经为这个创建了NSObject类现在检查代码。确认你已经声明了你正在使用的自定义类的所有变量。@Kukdeep i有一个AppointListing类,在这个类中这些都是,那么如何在那个数组中添加arObject呢
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]);
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];