我使用 C# 通过 axosoft 对软件调用 OnTime 进行 api 调用。
我在客户端创建了三个自定义字段:
定制_170
定制_171
定制_172
每个字段都被添加到 JSON 调用“custom_fields”中的一个部分。OnTime 提供了他们自己的 API 包装器,可以轻松使用他们的代码。
在下面的 C# 代码中,我使用 get 从“缺陷”中提取 JSON,然后循环查找缺陷号 7。
如果找到数字 7,它将从 JSON Id、Name、custom_170、custom_171、custom_172 中提取 5 个值。
我遇到的问题是我的程序找到了 Id、Name、custom_170,但查找 custom_171 的 if 语句从 custom_172 获取值,最后一个 if 似乎从未被触及(见下面的结果)。
我怎样才能从 custom_171 和 custom_172 中获取值,并将它们放在正确的位置?
JSON(片段)
{
"data": {
"reported_date": "2014-09-25T04:00:00Z",
"percent_complete": 100,
"archived": false,
"publicly_viewable": false,
"completion_date": null,
"due_date": null,
"description": "",
"name": "Defect Created from API Explorer 3",
"notes": "",
"number": "7",
"custom_fields": {
"custom_171": "Work Around Steps",
"custom_172": "Work Journal",
"custom_170": "Analysis"
}
}
}
C# 代码
var DefectInfo = axosoftClient.Defects.Get();
int? defectID = 0;
string defectName = "";
string defectAnalysis = "";
string defectWAS = "";
string defectWJ = "";
foreach (var defect in DefectInfo.Data)
{
if(defect.Id == 7)
{
defectID = defect.Id;
defectName = defect.Name;
if(defect.CustomFields.ContainsKey("custom_170"))
{
defectAnalysis = (string)defect.CustomFields["custom_170"];
}
if(defect.CustomFields.ContainsKey("custom_171"))
{
defectWAS = (string)defect.CustomFields["custom_171"];
}
if (defect.CustomFields.ContainsKey("custom_172"))
{
defectWAS = (string)defect.CustomFields["custom_172"];
}
}
}
Console.WriteLine("Defect ID: {0} Defect Name: {1}\nAnalysis: {2} \nWork Around: {3}\nWork Journal: {4}\n\n", defectID, defectName, defectAnalysis, defectWAS, defectWJ);
结果
Defect ID: 7 Defect Name: Defect Created from API Explorer 3
Analysis: Analysis
Work Around: Work Journal
Work Journal: