【问题标题】:IOS:how can i get a json dictionary in viewcontroller in iosIOS:如何在ios的viewcontroller中获取json字典
【发布时间】:2013-11-28 10:10:25
【问题描述】:

我是 ios 的新手。我使用了以下代码。我正在创建常量文件。这个文件我需要在程序中全部使用。 从那个文件我需要json字典。 我怎么能得到这个请帮助我。

 #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 字典 我该怎么办。

【问题讨论】:

  • 你应该使用单例。
  • 像共享实例一样使用

标签: ios iphone objective-c


【解决方案1】:

您可以将类设为 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];

它会帮助你。

【讨论】:

  • 先生没有得到您的答复
  • @user2914432 您想在整个应用程序中访问该 json 字典吗??
【解决方案2】:

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

@property (nonatomic, retain) NSArray *jsonArray;

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

然后你想访问它:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多