如何解析iphone sdk中的JSON数组?

时间:2011-12-14 06:38:46

标签: iphone objective-c ios json cocoa

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

-(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数组,但我不知道如何做到这一点 提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

    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"];
    }

希望它能给你一个想法。

答案 1 :(得分:0)