从Web响应中设置标签中的文本

时间:2015-12-15 12:08:00

标签: ios objective-c uilabel

我想设置一些文字,这些文字正在对标签进行网页回复。以下是我对网络服务的回复。

{
success = 1;
"user_post" = (
            {
        "associated_since" = "02 December 2015";
        "company_name" = "Amartam";
        "user_email" = "xxxx";
        "user_name" = "xxxxxxx";
        "user_phoneno" = 000000;
    }
);

我正在尝试在user_name中设置label的值,但标签中只有(。这是我试过的代码。

    NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
    NSDictionary *userPost =[response valueForKey:@"user_post"];
    NSString *userName  = [userPost valueForKey:@"user_name"];
    self.customerNameLabel.text = [NSString stringWithFormat:@"%@",userName];

2 个答案:

答案 0 :(得分:0)

NSString *userName  = [[[response objectForKey:@"user_post"] objectAtIndex:0] valueForKey:@"user_name"];

答案 1 :(得分:0)

你的user_post是包含字典的数组。

因此您需要获取数组并从这些数组中获取所需的字典。

NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
NSArray *userArray = [response valueForKey:@"user_post"];
NSDictionary *userPost =[userArray firstObject];
NSString *userName  = [userPost valueForKey:@"user_name"];
self.customerNameLabel.text = [NSString stringWithFormat:@"%@",userName];