如何在iphone sdk pase一个JSON数组? [英] How to pase a JSON array in iphone sdk?

查看:173
本文介绍了如何在iphone sdk pase一个JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录表单,用户只能使用有效的 memberID 密码登录。如果用户输入正确的搪瓷和密码,我得到一个结果字符串包含用户在注册过程中创建的用户信息,如果密码错误,则显示状态400 作为结果字符串,结果字符串是包含上述值的一个json数组,一件事是如果成功登录发生它给出 staus 200 以及用户信息,我的需要是从数组中检索状态消息,我需要在应用程序中验证它,如果登录成功(状态200 ),它需要重定向到主页;如果它是(状态400 ),它显示一个失败的登录消息。
我的代码:
EDit

I have a login form where the user can login only with the valid memberID and password. If the user enter correct enamel and password i get a result string contains the user information that the user created in the signup process, if it the password is wrong it shows the status 400 as the result string, the result string is the json array which contains one f the above values, one thing is the if the success login occur it gives the staus 200 along with the user information, my need is to retrieve the status message from the array and i need to validate it within the app, if the login success(status 200) it needs to be redirected to the main page; if it is(status 400) it shows a unsuccessful login message. my code: EDit

-(IBAction)_clicksbtnsignIN:(id) sender
{
    [_txtmemberId resignFirstResponder];
    [_txtpassword resignFirstResponder];

    NSString *connectionstring =  [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];

    if ([connectionstring length]==0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"you are not connected to the internet" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        [alert show];
        [alert release];        
    }       
    else
    {
        //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSString *emailString = _txtmemberId.text; // storing the entered email in a string.            
        // Regular expression to checl the email format.

        NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailReg];
        //[pool drain];

        if (_txtmemberId.text.length == 0  || _txtpassword.text.length == 0) {
            UIAlertView *alertblnk = [[UIAlertView alloc]initWithTitle:@"ALERT" message:@"Fill the required text fields" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];

            [alertblnk show];

            [alertblnk release];
        }
        if (([emailTest evaluateWithObject:emailString] != YES) || [emailString isEqualToString:@""])
        {
            UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:@" Alert" message:@"Invalid Email ID" delegate:self
                                                       cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [loginalert show];

            [loginalert release];
        } 
        else {

            [_spinner startAnimating];

            NSString *uname = _txtmemberId.text;

            NSString *pwd   = _txtpassword.text;

            NSString *urlVal = @"http://dev.eltouchapps.net/api/?app=1&type=m1&action=t2&var1=";

            NSString *urlVal1 = [urlVal stringByAppendingString:uname];

            NSString *urlVal2 = [urlVal1 stringByAppendingString:@"&var2="];

            NSString *urlVal3 = [urlVal2 stringByAppendingString:pwd];              

            NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)urlVal3,NULL,               (CFStringRef)@"\n" "",kCFStringEncodingUTF8 );
            NSURL *url = [NSURL URLWithString:encodedString];

            NSString *resultString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

            UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:@" Message" message:resultString delegate:self
                                                       cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [loginalert show];                
           [loginalert release];

            lblresult.text = resultString;
            NSString *responseString = [resultString responseString]; 
            NSLog(@"Got Profile: %@", responseString); 

            NSMutableDictionary *responseJSON = [responseString JSONValue]; 
            NSString *firstName;
            if ([[responseJSON valueForKey:@"Status"] isEqualToString:@"200"]) // if success
            {

                ParallelReadViewController *detailViewController = [[ParallelReadViewController alloc] initWithNibName:@"ParallelReadViewController" bundle:nil];

                //detailViewController.firstString = firstString;
                // ...
                // Pass the selected object to the new view controller.

                [self.navigationController pushViewController:detailViewController animated:YES];

                [detailViewController release];
                // do something
                firstName = [responseJSON valueForKey:@"FirstName"];
            }
            }   
}
}

结果字符串是为什么我从服务器。我知道有解析JSONB数组我们想要,但我不知道如何做这个。

提前感谢。

Result string is why i get from the server. I know there is parsing of JSONB array we want , but i didn't know how to done this.
Thanks in advance.

推荐答案

根据您的回应假设,请尝试下面的代码

based on assumption of your response , try below code

    NSString *resultString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];        
    NSMutableDictionary *responseJSON = (NSMutableDictionary *)[responseString JSONValue]; 
    NSString *firstName;
    if ([[responseJSON valueForKey:@"Status"] isEqualToString:@"200"]) // if success
    {
        // do something
        firstName = [responseJSON valueForKey:@"FirstName"];
    }

希望它给你一个想法。

这篇关于如何在iphone sdk pase一个JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆