简体   繁体   English

使用C#在Jira-Rest-Api中搜索过滤器

[英]Searching for Filter in Jira-Rest-Api with C#

I am trying to search all favourite Filters (JIRA) from the actual User using c# HttpWebRequest and Rest-Api. 我试图使用c#HttpWebRequest和Rest-Api从实际用户搜索所有喜欢的过滤器(JIRA)。 I am still able to read Issues but the filters aren't working. 我仍然可以阅读问题,但过滤器无法正常工作。

Reading Issues works as follows: 阅读问题的工作原理如下:

For example I have this URL to get all Issues from project IT: 例如,我有这个URL来从项目IT获取所有问题:

http://jira-test.myServer.de/rest/api/2/search?jql=project=%22IT%22

I am using DataContractJsonSerializer to swap the JSON Response to C#-Objects. 我使用DataContractJsonSerializer将JSON响应交换到C# - 对象。

From this class I am getting an object after Serialization: 从这个类我得到序列化后的对象:

[DataContract]
internal class Kopf
{
    [DataMember]
    public string startAt = string.Empty;

    [DataMember]
    public string maxResults = string.Empty;

    [DataMember]
    public string total = string.Empty;

    [DataMember]
    public Issues[] issues = null;
}

The first lines of JSON are looking like this: JSON的第一行看起来像这样:

{"expand":"schema,names","startAt":0,"maxResults":50,"total":23044,"issues":[{"expand":"operations,editmeta,changelog,transitions,renderedFields","id":"40000","self":"http://jira-test.myServer.de/rest/api/2/issue/40000","key":"IT-23237","fields":

So I can't understand why the following isn't working for me: This URL give me the right JSON in Browser: 所以我无法理解为什么以下内容对我不起作用:这个URL在浏览器中为我提供了正确的JSON:

http://jira-test.myServer.de/rest/api/2/filter/favourite

First lines of JSON: 第一行JSON:

[{"self":"http://jira-test.myServer.de/rest/api/2/filter/10119","id":"10119","name":"Aktiv","description":"Alle Aufgaben die gerade aktiv von mir bearbeitet werden.","owner":{"self":"http://jira-test.myServer.de/rest/api/2/user?username=sb9923","key":"sb9923","name":"sb9923","avatarUrls":{"16x16":"http://jira-test.myServer.de/secure/useravatar?

And here is my Object which I want to serialize: 这是我想要序列化的对象:

[DataContract]
internal class FilterData
{
    [DataMember]
    public FilterKopf[] filter = null;
}

[DataContract]
internal class FilterKopf
{
    [DataMember]
    public string id = string.Empty;

    [DataMember]
    public string name = string.Empty;

    [DataMember]
    public string description = string.Empty;

    [DataMember]
    public string jql = string.Empty;
}

I don't get any Exception or something but the FilterKopf Array in the FilterData-Object is always null. 我没有得到任何异常或其他东西,但FilterData-Object中的FilterKopf数组始终为null。

I hope someone can help me with this. 我希望有人可以帮助我。 I think my C#-Class is the problem because the JSON seems fine and my browser gives the right output. 我认为我的C#-Class是问题,因为JSON似乎很好,我的浏览器提供了正确的输出。

If I understand right your problem is that the result contains an array of "Filter" objects but you want to deserialize it as a simple object containing the array. 如果我理解正确你的问题是结果包含一个“过滤器”对象数组,但你想将它反序列化为一个包含数组的简单对象。 So all you need is to deserialize the stream as FilterKopf[] instead of FilterData . 所以你需要的是将流反序列化为FilterKopf[]而不是FilterData

I created a simple request based on this answer (I modified it slightly, eg not POST but GET) 我根据这个答案创建了一个简单的请求(我稍微修改了一下,例如不是POST而是GET)

public class JiraTest
{
    internal IEnumerable<FilterKopf> GetFavouriteFilters()
    {
        string url = "http://jira-test.myserver.de/rest/api/2/filter/favourite";

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
        httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("YOUR_USERNAME:YOUR_PASSWORD"));

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FilterKopf[]));
        var filterKoepfe = (FilterKopf[])serializer.ReadObject(httpResponse.GetResponseStream());

        return filterKoepfe;
    }
}

[DataContract]
internal class FilterKopf
{
    [DataMember]
    public string id = string.Empty;

    [DataMember]
    public string name = string.Empty;

    [DataMember]
    public string description = string.Empty;

    [DataMember]
    public string jql = string.Empty;
}

With my own account and with my access to our Jira server the results really reflected my favourite filters. 使用我自己的帐户并访问我们的Jira服务器,结果真的反映了我最喜欢的过滤器。

Update 更新

As a second chance, try to use Json.NET instead of DataContractJsonSerializer. 作为第二次机会,尝试使用Json.NET而不是DataContractJsonSerializer。 Add to the project through NuGet , and replace the two rows of deserialization to these: 通过NuGet添加到项目中,并将这两行反序列化替换为:

FilterKopf[] filterKoepfe = null;
using (Stream stream = httpResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
    string jsonResponse = reader.ReadToEnd();
    filterKoepfe = Newtonsoft.Json.JsonConvert.DeserializeObject<FilterKopf[]>(jsonResponse);
}

Let's take a look what this does. 我们来看看这是做什么的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM