0
(
        {
        "first_name" = Akash;
        idprofile = 1;
        iduser = 1;
        "last_name" = Testing;
        picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/1_Jellyfish.jpg";
        "profile_picture_filepath" = "1_Jellyfish.jpg";
    },
        {
        "first_name" = testing;
        idprofile = 3;
        iduser = 1;
        "last_name" = tst;
        picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/3_Penguins.jpg";
        "profile_picture_filepath" = "3_Penguins.jpg";
    },
        {
        "first_name" = test;
        idprofile = 4;
        iduser = 1;
        "last_name" = test;
        picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/4_Chrysanthemum.jpg";
        "profile_picture_filepath" = "4_Chrysanthemum.jpg";
    },
        {
        "first_name" = prashant1;
        idprofile = 19;
        iduser = 1;
        "last_name" = kharade1;
        picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/19_Koala.jpg";
        "profile_picture_filepath" = "19_Koala.jpg";
    },
        {
        "first_name" = Priyank;
        idprofile = 68;
        iduser = 1;
        "last_name" = Jain;
        picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/68_P.jpg";
        "profile_picture_filepath" = "68_P.jpg";
    },
        {
        "first_name" = sdasd;
        idprofile = 106;
        iduser = 1;
        "last_name" = sdasd;
        picUrl = "http://qalina.acapglobal.com/kangatime/ktapp/profilepics/1/";
        "profile_picture_filepath" = "<null>";
    }
)

这是我的 json 响应数据。我想在表格视图中显示基于“idprofile”键的六个配置文件(6 个字典),其中包含 first_name、last_name 和配置文件图像。我越来越糊涂了。请帮忙?

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    AppDelegate *appDel = [[UIApplication sharedApplication]delegate];


    navBar=[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    [navBar setTintColor:[UIColor colorWithRed:0.0f/255.0f green:82.0f/255.0f blue:151.0f/255.0f alpha:1.0f]];
    UIImageView *myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(87, 3, 134, 33)];
    myImageView.image=[UIImage imageNamed:@"sprofile.png"];
    [navBar addSubview:myImageView];
    //[myImageView release];

    [self.view addSubview:navBar];

    UIButton *logOutButton=[UIButton buttonWithType:UIButtonTypeCustom] ;
    [logOutButton setFrame:CGRectMake(244, 10, 71, 32)];
    //[logOutButton setTitle:@"Log out" forState:UIControlStateNormal];
    [logOutButton setImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal];
    [logOutButton addTarget:self action:@selector(goToLoginScreen) forControlEvents:UIControlEventTouchUpInside];
    [navBar addSubview:logOutButton];

    UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 320, 30)];
    [headerLabel setBackgroundColor:[UIColor colorWithRed:210.0f/255.0f green:221.0f/255.0f blue:228.0f/255.0f alpha:1.0f]];
    [headerLabel setText:@"Currently selected profile: Please select"];
    [headerLabel setTextAlignment:NSTextAlignmentCenter];
    [headerLabel setFont:[UIFont fontWithName:@"helvetica" size:14.0f]];
    [headerLabel setTextColor:[UIColor colorWithRed:78.0f/255.0f green:126.0f/255.0f blue:70.0f/255.0f alpha:1.0f]];
    [self.view addSubview:headerLabel];

    homeTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 81, 320, 367) style:UITableViewStylePlain];
    [homeTable setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
    homeTable.delegate=self;
    homeTable.dataSource=self;
    homeTable.rowHeight=100;
    [self.view addSubview:homeTable];

    profileArray = [[NSMutableArray alloc]init];



    [self callWebService];




}

-(void)goToLoginScreen
{
    AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.window.rootViewController dismissViewControllerAnimated:YES completion:nil];



}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [profileArray count];
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    selectButton.frame = CGRectMake(100, 30, 63, 32);
    //[selectButton setBackgroundColor:[UIColor yellowColor]];
    [selectButton setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal];
    //[selectButton addTarget:self action:@selector(selectButtonSelected: ) forControlEvents:UIControlEventTouchUpInside];
    selectButton.tag = indexPath.row;

    [cell.contentView addSubview:selectButton];

//    NSDictionary *item = [profileArray objectAtIndex:[indexPath row]];
//    [[cell textLabel] setText:[item objectForKey:@"first_name"]];

    //    Home *tempHomeObj=(Home *)[profileArray objectAtIndex:indexPath.row];
    //    NSString *fName = (NSString *)tempHomeObj.fName;
    //    NSLog(@"fnameeeee==>%@",fName);


    //NSLog(@"fName==>%@",appDel.fName);

    AppDelegate *appDel=(AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSLog(@"profileeeee==>%@",profileArray);

   // NSMutableString *tempStr = (NSMutableString *)profileArray;

    cell.textLabel.text = [self.profileArray objectAtIndex:indexPath.row];

    //cell.imageView.image = appDel.tempUserImage;


    return cell;
}


-(void)callWebService
{
    //adding the indicator view to show that data is being processed.......
    indicatorView=[[UIView alloc] initWithFrame:CGRectMake(110  ,180,100 , 100)];
    indicatorView.tag=3000;
    [indicatorView setAlpha:0.7];
    indicatorView.layer.cornerRadius=15;
    indicatorView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:indicatorView];

    //now adding the activity indicator
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    //activityIndicator.frame = CGRectMake(20.0,20.0, 40.0, 40.0);
    //activityIndicator.center = indicatorView.center;
    activityIndicator.center=CGPointMake(50, 50);
    [activityIndicator startAnimating];
    [[self.view viewWithTag:3000]addSubview:activityIndicator];


   // AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    NSURL *url = [NSURL URLWithString:@"http://qalina.acapglobal.com/kangatime/kangatime_api/api/user/all_profiles/format/json"];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request addPostValue:@"1" forKey:@"id"];
    //[request addPostValue:pwdField.text forKey:@"pass"];

    [request setDelegate:self];
    [request startAsynchronous];

}


- (void)requestFinished:(ASIHTTPRequest *)request
{

    // Use when fetching text data
    NSString *responseString = [request responseString];
   // NSLog(@"responsestring==%@",responseString);
    NSMutableArray *responseArr= [responseString JSONValue];
    NSLog(@"responseArr==>%@",responseArr);
    NSDictionary *tempDict = [responseArr objectAtIndex:0];
    //NSLog(@"tempDict==>%@",tempDict);


    if(indicatorView!=nil)
    {
        [indicatorView removeFromSuperview];
        indicatorView=nil;
    }

    for(NSDictionary *dict in tempDict)
    {
        Home *objHome=[[Home alloc] init];
        AppDelegate *appDel = [[UIApplication sharedApplication]delegate];
       appDel.fName = [tempDict objectForKey:@"first_name"];
        NSLog(@"appDel.fName==>%@",appDel.fName );

        appDel.lName = [tempDict objectForKey:@"last_name"];
        //NSLog(@"lName==>%@",lName);

        NSMutableString *myStr=[NSMutableString stringWithString:appDel.fName];
        [myStr appendFormat:@" "];
        [myStr appendString:appDel.lName];
        objHome.userName=myStr;

        NSLog(@"objHome.userName==>%@",objHome.userName);

        NSString *idProfile = [tempDict objectForKey:@"idprofile"];
        NSLog(@"idProfile==>%@",idProfile);
//        NSString *imageStr=[tempDict objectForKey:@"profile_picture_filepath"];
//        NSMutableString *urlString= [NSMutableString stringWithString:@"http://qalina.acapglobal.com/kangatime/ktapp/profilepics/"];
//        [urlString appendString:imageStr];
//        NSURL *mainURL=[[[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]] autorelease];
//       // NSLog(@"mainURL==>%@",mainURL);
//
//      appDel.tempUserData = [[NSData alloc] initWithContentsOfURL:mainURL];
        //NSLog(@"tempUserData==>%@",tempUserData);

       // Home *objHome=[[Home alloc] init];


         //appDel.tempUserImage = [[UIImage alloc] initWithData:appDel.tempUserData];

        [profileArray addObject:objHome.userName];
       // [profileArray addObject:appDel.tempUserImage];

        NSLog(@"profileArray==>%d",[profileArray count]);
    }

    //[profileArray addObject:]

//    [homeTable setDataSource:self];
//    [homeTable setDelegate:self];
    [homeTable reloadData];

    //[self setProfileArray:[tempDict objectForKey:@"first_name"]];

}
4

2 回答 2

0

首先你已经解析了 json。要解析 jason,您可以使用JSONKit

于 2012-10-15T06:20:53.870 回答
0

在 JSONKIT 的帮助下,首先你必须解析响应字符串。

您将从 JSON 响应中获取数组或字典。@SmartWork 为您提供了 JSONKIT 的链接以及如何创建下面给出的 JSON 数组字符串?将帮助您从响应中检索数组/字典。

于 2012-10-17T06:49:20.960 回答