iPhone:ios 5中的json教程

我是第一次在我的应用程序中实现json.

在我的应用程序中,我必须将参数从我的应用程序传递到webservice,并且根据我可以从Web服务获得响应,但我无法找到任何可以在ios5中从我的应用程序传递参数的教程

那么网上有什么教程,我可以从中获得对我有用的教程.

我试图找到使用谷歌,但我没有成功.

最佳答案 以下步骤将json集成到我们的项目中.

1)将以下文件复制粘贴到您的项目中.

2)创建新的组名作为帮助,其中包含以下文件.

3)将新文件创建为Support Files,其名称为constant.h

#define WEB_SERVICE_URL @"Your server url" //@"http://demo.google.com/webservice/"

#define USERNAME @"Your user id"//static username to send everytime
#define PASSWORD @"Your Password"//static password to send everytime

4)将上面的变量链接更改为您的Web服务所在的位置,这意味着您的Web服务的URL“WEB_SERVICE_URL”

5)打开“Rest.h”文件并创建一个方法.

-(void)Get_StoreDetails:(SEL)seletor andHandler:(NSObject*)handler andCountryId:(NSString *)CountryCode;

6)打开“Rest.m”文件和哪个参数传递给服务器该方法进行编码.

-(void)Get_StoreDetails:(SEL)seletor andHandler:(NSObject *)handler andCountryId:(NSString *)country_id{
    NSDictionary *req = [NSDictionary dictionaryWithObjectsAndKeys:@"content/get_stores",@"request",
                         [NSDictionary dictionaryWithObjectsAndKeys:USERNAME,@"username",
                          PASSWORD,@"password",
                          country_id,@"country_id",
                          nil],@"para",nil];
    [[[JSONParser alloc] initWithRequest:req sel:seletor andHandler:handler] autorelease];

}

7)现在Rest应用程序已准备好发送请求,然后此方法调用我们的视图控制器.

下面的代码放入.h文件中的“viewWillAppear”方法

#import <UIKit/UIKit.h>

@interface JsonDemoAppViewController : UIViewController{
    UIAlertView *alertCtr;
}

@property(nonatomic,retain)NSMutableArray *arrForStores;
- (void)getData:(id)object;
@end

下面的代码放入.m文件中的“viewWillAppear”方法并导入“Rest.h”.

- (void)viewWillAppear:(BOOL)animated {
    self.arrForStores=nil;
    //============================Json Initialization===================================================
    REST *rest = [[REST alloc] init];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [rest Get_StoreDetails:@selector(getData:) andHandler:self andCountryId:@"12"];

    alertCtr = [[[UIAlertView alloc] initWithTitle:@"\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
    [alertCtr show];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    // Adjust the indicator so it is up a few pixels from the bottom of the alert
    indicator.center = CGPointMake(alertCtr.bounds.size.width / 2, alertCtr.bounds.size.height - 50);
    [indicator startAnimating];
    [alertCtr addSubview:indicator];
    [indicator release];
    //===================================================================================================

}

然后回复进入服务器,时间对象存储到变量中,那时我们创建调用的metod.

- (void)getData:(id)object{
    NSLog(@"%@",object);
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSLog(@"Controll comes here..");
        //IF your response type is NSDictionary then this code to run
    }

    if ([object isKindOfClass:[NSArray class]])
    {
        self.arrForStores=[NSArray arrayWithArray:(NSArray*)object];
        //IF your response type is NSArray then this code to run
    }

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [alertCtr dismissWithClickedButtonIndex:0 animated:YES];

}

Download the source code From Here

点赞