-1

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

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

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

如果您看到下面的照片,您会看到 Brithouse,它是 json 调用中的第四个客户端。我想在 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

在此处输入图像描述

4

3 回答 3

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];
于 2013-05-16T11:16:31.307 回答
0

你可以用这个..

循环之前:

NSString *tempString = @"";

在循环:

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

循环后:

jsonSummary.text = [NSString stringWithFormat:@"The clients under this user are: %@", tempString];
于 2013-05-16T11:14:22.057 回答
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];
于 2013-05-16T11:23:03.990 回答