c# - 如果其他条件在 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"
}
解决方案
是的。我也有点卡在那个部分。我尝试通过添加 && 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 );
...
}
}
在将日期与任何内容进行比较之前,您还应该检查以确保成功解析日期。
推荐阅读
- c++ - 为什么 std::foreach 不能与 std::vector 一起使用
? - elasticsearch - 匹配同一字段中的多个值
- java - 如何在 StreamingOutput 的 Web 服务中保持客户端和服务器连接处于活动状态
- c++ - 在 h 文件上内联发布版本和在 cpp 上实现调试版本的函数
- angular - Angular Web应用程序-打字稿http数据问题
- python-3.5 - 使用 python3 对文件进行编码和上传到服务器的最佳方法是什么?
- android - 无法创建简单的数据库
- apache-kafka - 如何使用 Vertx Pump() 将文件流连接到 Kafka 生产者流?
- ckeditor - ckeditor 删除带有列表的内容
- javascript - 这个函数如何反转这个字符串?