我使用C#在axosoft的软件调用OnTime上进行api调用。
我在客户端中创建了三个自定义字段:
custom_170
custom_171
custom_172
每个字段都会添加到JSON调用“ custom_fields”的一个部分中。OnTime提供了自己的API包装器,可轻松使用其代码。
在下面的C#代码中,我使用get从“ Defects”中提取JSON,然后循环遍历查找缺陷编号7。
如果找到数字7,它将从JSON ID,名称,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:
defectWAS
分配了两次,而defectWJ
没有分配新值
您可能的意思是:
if (defect.CustomFields.ContainsKey("custom_172"))
{
defectWJ = (string)defect.CustomFields["custom_172"];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句