我實現JSON解析如下:獲取方法JSON解析在IOS
-(void)getallEvent
{
SBJSON *json = [SBJSON new];
json.humanReadable = YES;
responseData = [[NSMutableData data] retain];
NSString *service = @"/GetAllVenue";
NSString *str;
str = @"Calagary";
NSString *requestString = [NSString stringWithFormat:@"{\"CityName\":\"%@\"}",str];
//NSLog(@"request string:%@",requestString);
// NSString *requestString = [NSString stringWithFormat:@"{\"GetAllEventsDetails\":\"%@\"}",service];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc = [urlLoc stringByAppendingString:service];
//NSLog(@"URL : %@",urlLoc);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
// self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
if (respError)
{
NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",
[respError localizedDescription],
[[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Check your network connection" message:msg delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSDictionary *results = [[responseString JSONValue] retain];
//NSLog(@" %@",results);
NSString *extractUsers = [[results objectForKey:@"d"] retain];
NSDictionary *finalResult = [[extractUsers JSONValue] retain];
NSLog(@"Final Results : %@",finalResult);
listOfEvents = [finalResult objectForKey:@"List of Event details of given Venue"];
}
使用此代碼,它減慢應用程序。我如何解析json在後臺? * 此方法適用於Post Method嗎? Post &獲取方法與有什麼不同? *
是否有任何其他方式json解析?
[POST和GET有什麼區別?](http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get) –