如何生成多行UILabel

时间:2013-05-16 11:06:27

标签: objective-c parsing uilabel

我有一个json调用,我将结果解析为UILabel。在我的json响应中,我在客户端下有4个名字。

我正在尝试将所有客户端放入我的UILabel。

目前我只看到json响应中的4个客户端中的最后一个,但在我的日志中我可以看到所有4个客户端。

如果您看到下面的照片,您会看到brithouse是json电话中的第4个客户。我想查看mu uilabel的所有4位客户?目前我只看到最后一个客户。

由于

{
   "ProfileID":34,
   "ProfilePictureID":20,
   "Name":"Bilbo Baggins",
   "Clients":
   [
      {
         "ClientID":91,
         "Name":"Fnurky"
      },
      {  
         "ClientID":92,
         "Name":"A different client"
      },
      {
         "ClientID":95,
         "Name":"Second Community"
      },
      {
         "ClientID":96,
         "Name":"Britehouse"
      }
   ]
}

我的目标C代码

NSDictionary* json1 = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                     options:kNilOptions
                                                       error:&error];


NSArray* latestLoans = [json1 objectForKey:@"Clients"]; //2

NSLog(@"Clients: %@", latestLoans); //3

NSArray *performersArray = [json1 objectForKey:@"Clients"];
for (NSDictionary *performerDic in performersArray) {
    NSLog(@"%@", [performerDic objectForKey:@"Name"]);

    jsonSummary.text = [NSString stringWithFormat:@"The clients under this user are: %@ ",
                         [performerDic objectForKey:@"Name"]];

的NSLog

2013-05-16 13:03:52.820 com.barloworld.atajo[5137:907] Fnurky
2013-05-16 13:03:52.821 com.barloworld.atajo[5137:907] A different client
2013-05-16 13:03:52.821 com.barloworld.atajo[5137:907] Second Community
2013-05-16 13:03:52.822 com.barloworld.atajo[5137:907] Britehouse

enter image description here

3 个答案:

答案 0 :(得分:1)

使用此

            NSDictionary* json1 = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                 options:kNilOptions
                                                   error:&error];
            NSArray* latestLoans = [json1 objectForKey:@"Clients"]; //2
            NSArray *performersArray = [json1 objectForKey:@"Clients"];
            NSMutableArray *array=[[NSMutableArray alloc]initWithCapacity:3];
            for (NSDictionary *performerDic in performersArray) {
                [array addObject:[performerDic objectForKey:@"Name"]];
            }
            NSString *output=[array componentsJoinedByString:@","];
            jsonSummary.text = [NSString stringWithFormat:@"The clients under this user are: %@ ",
                                output];

答案 1 :(得分:0)

你可以用这个......

循环之前

NSString *tempString = @"";

在循环中:

tempString = [tempString stringByAppendingFormat:@"%@", [performerDic objectForKey:@"Name"]];

循环后:

jsonSummary.text = [NSString stringWithFormat:@"The clients under this user are: %@", tempString];

答案 2 :(得分:0)

以下代码只会将最后一个值添加到label

jsonSummary.text = [NSString stringWithFormat:@"The clients under this user are: %@ ",
                     [performerDic objectForKey:@"Name"]];

您必须将每个客户端名称附加到字符串中,然后将字符串值显示到标签中...要附加到字符串中,

JsonString =[NSString stringByAppendingFormat:   [performerDic objectForKey:@"Name"]]; 

现在,此字符串将包含所有客户端值,现在

  jsonSummary.text =[NSString stringWithFormat : @"YOUR OWN LINES %@ " JsonString];