Team Services API中的工作项的正则表达式

时间:2017-06-27 14:56:56

标签: regex azure-devops tfs-workitem azure-devops-rest-api

我正在使用VSTS API检索工作项列表,并希望在我的网络应用中显示它们。我可以使用以下格式成功返回工作项列表:

{ “计数”:1, “值”:[{ “ID”:246, “REV”:4 “字段”:{ “System.Id”:246, “System.State”: “新” “System.Title”: “测试1”}, “URL”: “https://example.visualstudio.com/_apis/wit/workItems/246”}]}

我尝试使用正则表达式从此HTTP响应中获取值,并使用以下代码:

HttpResponseMessage getWorkItemsHttpResponse = client.GetAsync("_apis/wit/workitems?ids=" + ids + "&fields=System.Id,System.Title,System.State&asOf=" + workItemQueryResult.asOf + "&api-version=2.2").Result;

                    if (getWorkItemsHttpResponse.IsSuccessStatusCode)
                    {
                        result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;

                        // Regular expression to extract work item values to display    
                        string parseWI = result.ToString();
                        var match = Regex.Match(parseWI, "\"System.ID\": (.*)");
                        workItemsToDisplay = (match.Groups[1].Value);


                    }
                }
            }
        }
        return workItemsToDisplay;

    }

这是拒绝返回任何内容然后离开文本框我将workItemsToDisplay显示为空。我不熟悉正则表达式,我确信这是问题的源头。不确定Microsoft是否已有示例代码来构建响应中的工作项显示。

1 个答案:

答案 0 :(得分:0)

不要使用正则表达式。那个JSON,使用JSON解析库(JSON.Net是.NET世界中事实上的标准),然后您可以轻松地检索特定字段。