简体   繁体   中英

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. 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:

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.

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:

{"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:

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

First lines of 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.

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.

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 .

I created a simple request based on this answer (I modified it slightly, eg not POST but 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.

Update

As a second chance, try to use Json.NET instead of DataContractJsonSerializer. Add to the project through NuGet , and replace the two rows of deserialization to these:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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