我想设置一些文字,这些文字正在对标签进行网页回复。以下是我对网络服务的回复。
{
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];
答案 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];