我新ios.i使用下面的code.i創建常量file.this文件我需要遍佈程序。 從那個文件我需要json字典。 我怎麼能得到這個PLZ幫助我。IOS:我怎樣才能得到一個JSON字典在視圖控制器在ios
#import "constFile.h"
#import "SBJsonParser.h"
@implementation constFile
- (void) alertStatus:(NSString *)msg :(NSString *)title
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alertView show];
}
-(void)jsonPost:(NSString *)string
{
NSString *loginJson=[NSString stringWithFormat:@"data=%@",string];
NSLog(@"jsonstring%@",loginJson);
NSURL *url=[NSURL URLWithString:@"http://lbwt-sl-745515119.ap-southeast- 1.elb.amazonaws.com:8080/wsserver.php"];
NSData *postData = [loginJson dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [[SBJsonParser alloc]init];
json = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",json);
} else
{
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Login Failed!"];
}
}
@end
我想訪問JSON字典中所有的程序 我該怎麼辦。
您應該使用單身。 –
使用像一個sharedInstance – codercat