IOS:如何在ios中的viewcontroller中获取json字典

时间:2013-11-28 10:10:26

标签: ios iphone objective-c

我是ios.i的新手使用以下代码。我正在创建常量file.this文件我需要在程序中全部。 从那个文件我需要json字典。 我怎么能得到这个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

我希望访问json字典一个所有程序  我该怎么办。

2 个答案:

答案 0 :(得分:0)

您可以将类设为UserdataSingleton,您可以在整个应用程序中使用它。 此代码模板可以帮助您:

#import <Foundation/Foundation.h>

@interface UserDataSingleton : NSObject
{
    @private
    NSDictionary *globalDictionary;

}

+(UserDataSingleton *) getInstance;
-(void)saveInUserDatasingletonWithDictionary:(NSDictionary *)dictionary;
-(NSDictionary *)getGlobalDictionary;

@end

和实现文件会是这样的:

#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

================== 用法:

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

现在访问json,假设你有YourViewController类。

NSMutableArray *yourArrayFromWebResponse = [USERDATASINGLETON getGlobalDictionary];

它会帮助你。

答案 1 :(得分:0)

在你的appdelegate中定义一个属性,即

@property (nonatomic, retain) NSArray *jsonArray;

然后在appDidFinishLaunchingWithOptions中将json分配给该属性

然后你想要访问它:

AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
NSString *aString = [appDelegate.jsonArray objectForKey:@"any_key"];