JIRA REST API、JSON 响应,C#

JIRA REST API, JSON response, C#

我想从看板上获取问题并打印问题摘要(使用 JIRA Rest API:https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board-getIssuesForBoard),但我不想获取所有问题的字段,- 我需要字段: "sprint" 和 "epic".

我有错误:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

行:

var response = client.RestClient.ExecuteRequest<ResponseIssues>(Method.GET, 
                        @"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");

代码(C#):

var client = Atlassian.Jira.Jira.CreateRestClient(jiraurl, login, password);
var response = client.RestClient.ExecuteRequest<ResponseIssues>(Method.GET, 
                       @"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");
foreach (var issue in response.issues)
{
     Console.WriteLine(issue.fields.epic.summary);
}

Class 响应问题 (C#):

public class ResponseIssues
{
    public string expand;
    public int startAt;
    public int maxResults;
    public int total;
    public List<Issue_> issues;
}    
public class Issue_
{
    public string expand;
    public string id;
    public string self;
    public string key;
    public Field fields;
}    
public class Field
{
    public Sprint sprint;
    public Epic epic;            
}   
public class Sprint
{
    public int id;
    public string self;
    public string state;
    public string name;
}    
public class Epic
{
    public int id;
    public string self;
    public string name;
    public string summary;
    public Color color;
    public bool done;
}    
public class Color
{
    public string key;
}

简单游记"issues"=>"fields"得到了你的"sprint"和"epic"。

 var response = client.RestClient.ExecuteRequest<ResponseIssues>(Method.GET, 
                        @"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");
            var jo = JObject.Parse(response);
            var id = jo["issues"]["fields"]['sprint']['id'].ToString();
            var self= jo["issues"]["fields"]['sprint']['self'].ToString();
            var state= jo["issues"]["fields"]['sprint']['state'].ToString();
            var name= jo["issues"]["fields"]['sprint']['name'].ToString();
            Console.WriteLine(id);
            Console.WriteLine(self);
            Console.WriteLine(state);
            Console.WriteLine(name);
            Console.Read();

好像有些东西没有正确初始化。每当这种情况发生在我身上时,我都会用赋值打断链条,试图找出哪个对象是空的。也捕获异常,并在 visual studio 中探索它。在你的情况下,试试这个:

try{
  var client = Atlassian.Jira.Jira.CreateRestClient(jiraurl, login, password);
  // Add breakpoint... is client null? There could be an issue with your login, password or the url you've passed. 
  // Or maybe it's Atlassian.Jira.CreateRestClient (instead of Jira.Jira.CreateRestClient), though that should get flagged by the compiler unless there's some recursion or parenting in the Jira namespace
  var restClient = client.RestClient; 
  // Add breakpoint... is restClient null? Could be that CreateRestClient is lazy-loading, in which case I'd look at the url, login and password again.
  var response = restClient.ExecuteRequest<ResponseIssues>(Method.GET, 
                   @"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");
}
catch(Exception ex)
{
  Console.WriteLine(ex); // Or just breakpoint here and explore the exception.
}