首页 > 解决方案 > 如果其他条件在 c# 中没有正确检查?

问题描述

我遇到了一个问题,我正在检查某些元素键值是否有效,以便继续进行 QR 码扫描。例如:如果我的 Active 属性为 true,且 completed 为 false,并且计划时间是当前时间 +- 开始窗口的 30 分钟,则继续扫描。如果没有,让我告诉他们一个错误。我尝试用一​​个简单的 if 来实现检查部分 - 但只检查活动的和已完成的键值。任何人都可以检查并帮助我解决这个问题。谢谢您的帮助。

这是我的代码:

     public void ScanQrCode()
     {

         BarcodeScanner.Scan(async (barCodeType, barCodeValue) =>
         {
             BarcodeScanner.Stop();


             var results = JsonConvert.DeserializeObject<dynamic>(barCodeValue);
             var gettingTheName = (string) results.Evaluation.Value;
             TextHeader.text = gettingTheName;
             var qrCodeString = $"***************.firebaseio.com/Evaluations/.json?orderBy=\"$key\"&startAt=\"{gettingTheName}\"&limitToFirst=1";
             Debug.Log(barCodeValue);

                           var qrCodeObj = JsonConvert.DeserializeObject<dynamic>(qrCodeString);


                       try
              {

                   bool parseSuccessful = DateTime.TryParse("ScheduleStartTime", out var scheduledStartTime);
                  if (results.Contains("Active").Equals(true) &&
                      results.Contains("Completed").Equals(false) &&
                      DateTime.Now < scheduledStartTime.AddMinutes(30) &&
                      DateTime.Now > scheduledStartTime.AddMinutes(-30)) {

                      var matchingLink = new WebClient().DownloadString(qrCodeString);
                      var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(matchingLink);
                      var candidateId = obj.First.First["CandiateID"].ToString();
                      string page = $"https://***********.firebaseio.com/Candidates/.json?orderBy=\"$key\"&startAt=\"{candidateId}\"&limitToFirst=1";

                      using (HttpClient client = new HttpClient())
                      using (HttpResponseMessage response = await client.GetAsync(page))
                      using (HttpContent content = response.Content)
                      {
                          // Reading the string. 
                          Dictionary<string, Candidates> evaluationDictionary = new Dictionary<string, Candidates>();
                          string result = await content.ReadAsStringAsync();
                          evaluationDictionary = JsonConvert.DeserializeObject<Dictionary<string, Candidates>>(result);
                          Debug.Log(evaluationDictionary);

                          foreach (Candidates candidates in evaluationDictionary.Values)
                          {
                              string evaluationMessage = candidates.FirstName + " " + candidates.LastName;
                              candidateMessage = GetComponent<Text>();
                              candidateMessage.text = evaluationMessage;

                          }


                          // Getting a reference to the text component.
                          candidateMessage = GetComponent<Text>();
                          candidateMessage.text = matchingLink.ToString();
                          candidateMessage.text = matchingLink.Trim(new char[] {'"'});

                      }

                  }
                  else
                  {
                      EditorUtility.DisplayDialog("Incorrect credentials", "Please scan a valid QR code", "OK");
                  }
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
                  SceneManager.LoadScene("Verify");
                  throw;
              }
          });
      }
  }

JSON:

    {
    "Active": true,
    "Completed": false,
    "ScheduleStartTime": "2019-12-16T20:10:57.649418-08:00"
} 

标签: c#

解决方案


是的。我也有点卡在那个部分。我尝试通过添加 && qrCodeString.Contains("ScheduleStartTime").Equals( I GOT STUCK IN HERE )来做到这一点

我将通过将日期字符串解析为 DateTime 对象来完成这部分,然后将当前时间与双向移动的新 DateTime 进行比较。

我可能会做这样的事情:

try {
                bool parseSuccessful = DateTime.TryParse( ScheduledStartTimeFromJSON, out var scheduledStartTime );
                if ( qrCodeString.Contains( "Active" ).Equals( true ) &&
                     qrCodeString.Contains( "CompletedMessage" ).Equals( false ) &&
                     DateTime.Now < scheduledStartTime.AddMinutes(30) && 
                     DateTime.Now > scheduledStartTime.AddMinutes( -30 ) ) {
                    var matchingLink = new WebClient().DownloadString( qrCodeString );
                    ...
                }
            }

在将日期与任何内容进行比较之前,您还应该检查以确保成功解析日期。


推荐阅读