[英]IOS:how can i get a json dictionary in viewcontroller in ios
I m new to ios.i used following code.i am creating constant file.this file i need all over in program. 我是ios.i的新手,使用了以下代码.i正在创建常量文件。此文件我在程序中都需要使用。 from that file i need json dictionary. 从该文件中,我需要json字典。 how i can get this plz help me out. 我怎样才能得到这个PLZ帮助我。
#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
i want access json dictionary an all program how can i do. 我想访问json词典所有程序,我该怎么办。
you can make class as UserdataSingleton which you can use all over your application. 您可以将类设为UserdataSingleton,可以在整个应用程序中使用。 this code template may help you: 此代码模板可以帮助您:
#import <Foundation/Foundation.h>
@interface UserDataSingleton : NSObject
{
@private
NSDictionary *globalDictionary;
}
+(UserDataSingleton *) getInstance;
-(void)saveInUserDatasingletonWithDictionary:(NSDictionary *)dictionary;
-(NSDictionary *)getGlobalDictionary;
@end
and implementation file will be some thing like: 和实现文件将是这样的:
#import "UserDataSingleton.h"
@implementation UserDataSingleton
static UserDataSingleton *userDataSingletonInstance;
+(UserDataSingleton *) getInstance
{
if (userDataSingletonInstance == nil) {
userDataSingletonInstance = [[UserDataSingleton alloc] init];
}
return userDataSingletonInstance;
}
-(void)saveInUserDatasingletonWithDictionary:(NSDictionary *)dictionary
{
globalDictionary = dictionary;
}
-(NSDictionary *)getGlobalDictionary
{
return globalDictionary;
}
@end
================== usage: ==================用法:
#import "constFile.h"
#import "SBJsonParser.h"
#import "UserDataSingleton.h"
#define USERDATASINGLETON (UserDataSingleton *)[UserDataSingleton getInstance]
......................your code...
. 。 .. ..
json = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",json);
[USERDATASINGLETON saveInUserDatasingletonWithDictionary:json];
Now to access json lets say you have YourViewController class. 现在访问json可以说您拥有YourViewController类。
NSMutableArray *yourArrayFromWebResponse = [USERDATASINGLETON getGlobalDictionary];
it will help you. 它会帮助你。
define a property in your appdelegate, ie 在您的appdelegate中定义一个属性,即
@property (nonatomic, retain) NSArray *jsonArray;
then assign you json to that property in appDidFinishLaunchingWithOptions 然后将您的json分配给appDidFinishLaunchingWithOptions中的该属性
then where you want to access it: 然后在您要访问它的地方:
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
NSString *aString = [appDelegate.jsonArray objectForKey:@"any_key"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.