IOS比较两个数组并获得结果

时间:2016-01-21 13:32:35

标签: ios objective-c arrays

我的JSON格式低于:

 {
  "Get_your_story_answer": [
    {
      "st_id": "19",
      "story_title": "Newone",
      "user_fb_id": "1649424812005217",
      "winner_fb_id": "1685417121693688",
      "winner_fb_id2": "",
      "winner_fb_id3": "",
      "story": "\ud83d\ude33\ud83d\ude25\ud83d\ude14\ud83d\ude0c",
      "status": "1",
      "s_created_date": "2016-01-18 23:06:05",
      "reply_answer": [
        {
          "l_id": "42",
          "story_id": "19",
          "user_fb_id": "1649424812005217",
          "selected_user_fb_id": "1685417121693688",
          "answer": "hahaha",
          "l_rating": "0",
          "status": "1",
          "l_created_date": "2016-01-18 23:10:51",
          "l_new_created_date": "2016-01-19 11:40:51",
          "winner": "1",
          "answer_user_id": "3",
          "answer_fb_id": "1685417121693688",
          "answer_user_name": "Kin Patty"
        },
        {
          "l_id": "43",
          "story_id": "19",
          "user_fb_id": "1649424812005217",
          "selected_user_fb_id": "1498304680499454",
          "answer": "",
          "l_rating": "0",
          "status": "1",
          "l_created_date": "2016-01-18 23:06:05",
          "l_new_created_date": "2016-01-19 11:36:05",
          "winner": "0",
          "answer_user_id": "10",
          "answer_fb_id": "1498304680499454",
          "answer_user_name": "John Kingman"
        }
      ]
    }
  ],
  "status": "1",
  "msg": "Get data"
}

现在我必须获得Winner id

来自"winner_fb_id"

示例 [Get_your_story_answer]并将其与[reply_answer]进行比较。

如果[reply_answer]包含"winner_fb_id",那么我只能将该名称作为获胜者。 就像我的例子中一样,“Kin Patty”

我试过了,

// _ getwind是可变数组

  _getwinnerid =[NSMutableArray new];
 _getwinnerid=[[_dataDictionary  valueForKey:@"Get_your_story_answer"]valueForKey:@"winner_fb_id"];

这是_getwinnerid输出

  

< __ NSArrayI 0x7ffb5a57c7b0>(   1685417121693688

   NSArray *replaydata=[[_dataDictionary  valueForKey:@"Get_your_story_answer"]valueForKey:@"reply_answer"];

这是重播数据结果

enter image description here

if (![replaydata containsObject:_getwinnerid]) {
    NSLog(@"data");
}

注意: - 获胜者ID将是多个。所以,我必须比较2个或更多的赢家ID,然后从重播数据响应中取名。然后我必须在UITableview中设置获胜者的名字。

3 个答案:

答案 0 :(得分:0)

Get_your_story_answer的值是一个数组,请求的数据位于数组的第一项

NSDictionary *getYourStoryAnswer = _dataDictionary[@"Get_your_story_answer"][0];

现在获得胜利者ID和答案数组

NSString *winnerID = getYourStoryAnswer[@"winner_fb_id"];
NSArray *answers = getYourStoryAnswer[@"reply_answer"];

然后枚举答案数组(还有其他解决方案使用块等)。

for (NSDictionary *answer in answers) {
   if ([answer[@"answer_fb_id"] isEqualToString: winnerID]) {
     NSLog(@"%@", answer[@"answer_user_name"]);
     break;
   }
}

PS:从不使用valueForKey从集合类型获取一个值,除非您确实需要(并且意味着)KVC方法。指定的方法是objectForKey或如上所述的密钥订阅。应用于数组的valueForKey总是返回一个通常不打算的数组。

答案 1 :(得分:0)

为此,您需要从服务器获得总赢家数。 像这样。

int winnerCount = 3; // [Get_your_story_answer valueForKey:@“winner_count”];

希望您为获胜者提供固定模式密钥,例如 winner_fb_id winner_fb_id1 winner_fb_id2 等。

这将为您提供阵列中获胜者名称的所需结果。

id response;  // your response here

NSArray *array =  [response objectForKey:@"Get_your_story_answer"];
NSDictionary *Get_your_story_answer = array[0];

NSMutableArray *winnersArray = [[NSMutableArray alloc] init];

int winnerCount = 3;  //[Get_your_story_answer valueForKey:@"winner_count"];

for (int i = 0; i< winnerCount; i++) {

    NSString *winnerId = nil;

    if(i != 0){
        winnerId = [Get_your_story_answer valueForKey:[NSString stringWithFormat:@"winner_fb_id%d",i]];
    }
    else{
       winnerId= [Get_your_story_answer valueForKey:[NSString stringWithFormat:@"winner_fb_id"]];
    }

    if(winnerId)
    {
        [winnersArray addObject:winnerId];
    }
}

__block NSMutableArray *resultArray = [[NSMutableArray alloc] init];

NSArray *replyAnswers = [Get_your_story_answer objectForKey:@"reply_answer"];

[replyAnswers enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {

    NSString *answer_fb_id = [obj valueForKey:@"answer_fb_id"];

    [winnersArray enumerateObjectsUsingBlock: ^(NSString * winnerId, NSUInteger idx, BOOL *stop) {

        if([winnerId isEqualToString:answer_fb_id]){

            // get name of winner
            [resultArray addObject:[obj valueForKey:@"answer_user_name"]];
        }

    }];
}];

// Your name of wiiners are in resultArray.

答案 2 :(得分:0)

我使用larme所说的答案过滤了数组。谢谢。

 NSError *errorJSON = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&errorJSON];
    if (!errorJSON)
    {
        NSDictionary *storyAnwser = [json[@"Get_your_story_answer"] firstObject];

        NSArray *allAnswersKeys = [storyAnwser allKeys];
        NSArray *allWinnersKeys = [allAnswersKeys filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
            return [evaluatedObject hasPrefix:@"winner_fb_id"];
        }]];
        NSLog(@"AllWinnersKeys: %@", allWinnersKeys);
        NSArray *allWinnersIds = [storyAnwser objectsForKeys:allWinnersKeys notFoundMarker:[NSNull null]];
        NSLog(@"AllWinnersIds: %@", allWinnersIds);

        NSArray *allAnswers = storyAnwser[@"reply_answer"];
        NSLog(@"AllAnswers: %@", allAnswers);
        NSArray *allWinners = [allAnswers filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"answer_fb_id IN %@", allWinnersIds]];
        NSLog(@"allWinners: %@", allWinners);
        NSArray *allWinersNames = [allWinners valueForKey:@"answer_user_name"];
        NSLog(@"AllWinnersNames: %@", allWinersNames);
    }
    else
    {
        NSLog(@"Error JSON: %@", errorJSON);
    }