4

I have JSON string returned by method:

public List<Issues> Getdata(
        string jql,
        List<string> fields = null,
        int startAt = 0,
        int maxResult = 500)
    {
        string data = JsonConvert.SerializeObject(request);
        string result = runQuery(JiraResource.search, data, "POST"); //returns results and working fine
        SearchResponse response = JsonConvert.DeserializeObject<SearchResponse>(result);
        return response.IssueDescriptions = GetIssues(response);;
    }

and JSON string look like this:

{"expand":"schema,names","startAt":0,"maxResults":50,"total":139,"issues":[{"expand":"operations,versionedRepresentations,editmeta,changelog,transitions,renderedFields","id":"20930","self":"https://clientname.atlassian.net/rest/api/2/issue/20930","key":"-139","fields":{"summary":"Rebate","issuetype":{"self":"https://clientname.atlassian.net/rest/api/2/issuetype/1","id":"1","description":"A fee negotiation or quote","iconUrl":"https://clientname.atlassian.net/images/icons/issuetypes/sales.png","name":"Fees","subtask":false},"components":[],"created":"2015-07-10T12:29:05.000+1000","description":"Execute as per attached instruction","project":{"self":"https://clientname.atlassian.net/rest/api/2/project/10013","id":"10013","key":"--","name":"Change Instructions","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/projectavatar?avatarId=10011","24x24":"https://clientname.atlassian.net/secure/projectavatar?size=small&avatarId=10011","16x16":"https://clientname.atlassian.net/secure/projectavatar?size=xsmall&avatarId=10011","32x32":"https://clientname.atlassian.net/secure/projectavatar?size=medium&avatarId=10011"}},"reporter":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=rajap","name":"rajap","key":"rajap","emailAddress":"email.au","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Prabu","active":true,"timeZone":"US"},"priority":{"self":"https://clientname.atlassian.net/rest/api/2/priority/3","iconUrl":"https://clientname.atlassian.net/images/icons/priorities/major.png","name":"Medium","id":"3"},"resolution":null,"duedate":"2015-07-15","comment":{"startAt":0,"maxResults":1,"total":1,"comments":[{"self":"https://clientname.atlassian.net/rest/api/2/issue/20930/comment/24290","id":"24290","author":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=acahill","name":"acahill","key":"acahill","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Andrew Cahill","active":true,"timeZone":"US"},"body":"John, please action","updateAuthor":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=acahill","name":"acahill","key":"acahill","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"Andrew","active":true,"timeZone":"US"},"created":"2015-07-10T12:35:43.728+1000","updated":"2015-07-10T12:35:43.728+1000"}]},"votes":{"self":"https://clientname.atlassian.net/rest/api/2/issue/-139/votes","votes":0,"hasVoted":false},"assignee":{"self":"https://clientname.atlassian.net/rest/api/2/user?username=olearyj","name":"olearyj","key":"olearyj","emailAddress":"email","avatarUrls":{"48x48":"https://clientname.atlassian.net/secure/useravatar?avatarId=10142","24x24":"https://clientname.atlassian.net/secure/useravatar?size=small&avatarId=10142","16x16":"https://clientname.atlassian.net/secure/useravatar?size=xsmall&avatarId=10142","32x32":"https://clientname.atlassian.net/secure/useravatar?size=medium&avatarId=10142"},"displayName":"John","active":true,"timeZone":"US"},"status":{"self":"https://clientname.atlassian.net/rest/api/2/status/1","description":"The issue is open and ready for the assignee to start work on it.","iconUrl":"https://clientname.atlassian.net/images/icons/statuses/open.png","name":"Open","id":"1","statusCategory":{"self":"https://clientname.atlassian.net/rest/api/2/statuscategory/2","id":2,"key":"new","colorName":"blue-gray","name":"To Do"}}}}]}

I wanted parser method for the above json string to get particular values of these field values like :

"summary", "status", "assignee", "comments", "description", "created", "duedate", "environment", "priority", "project", "reporter", "resolution", "issuetype", "Updated", "votes", "components"

I tried to get one by one.however, its complex and time taking.

This is how I'm trying. But error: Object reference not set to an instance of an object.

private List<JiraIssues> GetIssues(SearchResponse response)
{
    List<JiraIssues> returnResulttoReport = new List<JiraIssues>();
    foreach (var item in response.IssueDescriptions) //error here (not sure why IssueDescriptions is NULL but before call i'm de-serializing it)
    {
        foreach (var item1 in item.Comments)
        {

        }
    }

    return returnResulttoReport; 
}

My return type is List<Issuees>. Is there any library or example link to do this?

Please note that, I'm already de-serializing it, now I wanted perticular list from that.Also, please note that the JSON full string I have given is not same as the actual I have.But its similar. Please guide.

4
  • 1
    Well I'd use JsonConvert again, this time deserializing... or use LINQ to JSON. There are lots of resources about parsing JSON - what have you tried so far, and what was the problem? (It doesn't help that the only JSON array in your example is within GlossSeeAlso - GlossList really isn't a list... Commented Jul 21, 2015 at 6:09
  • Serialize this string to JSON, and use JSON.net to get values. See hint at: stackoverflow.com/questions/31321513/… Commented Jul 21, 2015 at 6:58
  • plus the json posted is invalid. Commented Jul 21, 2015 at 7:08
  • I have updated my question, what I have tried. Also, I wanted to use Newtonsoft as I have already used that in some methods. If this is complex in newtonsoft, then I will choose another. Commented Jul 21, 2015 at 7:31

2 Answers 2

2

Use something like json2csharp to generate C# classes for the json, to prevent errors. Then do something like

var result = JsonConvert.DeserializeObject<RootObject>(myJsonString);

EDIT
Here is an example of how to get the summaries:

RootObject result = JsonConvert.DeserializeObject<RootObject>(json);

List<string> summaries = new List<string>();
foreach (var item in result.issues)
{
    var summary = item.fields.summary;
    Console.WriteLine(summary);
    summaries.Add(summary);
}

RootObject will represent the top level object of your JSON and then it will have properties which correspond to each of the properties of your JSON.

Sign up to request clarification or add additional context in comments.

4 Comments

Here I wanted to get something which is very internal in the "result". How to fetch that?
Thanks for your response. However, I have "result.issues" = 150. So it will keep on going in foreach and return me the last. But, I want to get summary for each item.May be I have to return a list(becoz, I have to return so many things like summary).
Updated to show an example of adding them to a list
I'm trying to get, author name : "acahill" and displayName : "Andrew Cahill" from "Comments" like this "Console.WriteLine(item.fields.comment.comments.Comment2." However, getting error , it seems not recognizing Comment2. However, Comment2 is present in root class. Any suggession? Plaese edit you answer.
0

you can try like this :

    public class Issuetype
    {
        public string self { get; set; }
        public string id { get; set; }
        public string description { get; set; }
        public string iconUrl { get; set; }
        public string name { get; set; }
        public bool subtask { get; set; }
    }

    public class AvatarUrls
    {
        public string __invalid_name__48x48 { get; set; }
        public string __invalid_name__24x24 { get; set; }
        public string __invalid_name__16x16 { get; set; }
        public string __invalid_name__32x32 { get; set; }
    }

    public class Project
    {
        public string self { get; set; }
        public string id { get; set; }
        public string key { get; set; }
        public string name { get; set; }
        public AvatarUrls avatarUrls { get; set; }
    }

    public class AvatarUrls2
    {
        public string __invalid_name__48x48 { get; set; }
        public string __invalid_name__24x24 { get; set; }
        public string __invalid_name__16x16 { get; set; }
        public string __invalid_name__32x32 { get; set; }
    }

    public class Reporter
    {
        public string self { get; set; }
        public string name { get; set; }
        public string key { get; set; }
        public string emailAddress { get; set; }
        public AvatarUrls2 avatarUrls { get; set; }
        public string displayName { get; set; }
        public bool active { get; set; }
        public string timeZone { get; set; }
    }

    public class Priority
    {
        public string self { get; set; }
        public string iconUrl { get; set; }
        public string name { get; set; }
        public string id { get; set; }
    }

    public class AvatarUrls3
    {
        public string __invalid_name__48x48 { get; set; }
        public string __invalid_name__24x24 { get; set; }
        public string __invalid_name__16x16 { get; set; }
        public string __invalid_name__32x32 { get; set; }
    }

    public class Author
    {
        public string self { get; set; }
        public string name { get; set; }
        public string key { get; set; }
        public string emailAddress { get; set; }
        public AvatarUrls3 avatarUrls { get; set; }
        public string displayName { get; set; }
        public bool active { get; set; }
        public string timeZone { get; set; }
    }

    public class AvatarUrls4
    {
        public string __invalid_name__48x48 { get; set; }
        public string __invalid_name__24x24 { get; set; }
        public string __invalid_name__16x16 { get; set; }
        public string __invalid_name__32x32 { get; set; }
    }

    public class UpdateAuthor
    {
        public string self { get; set; }
        public string name { get; set; }
        public string key { get; set; }
        public string emailAddress { get; set; }
        public AvatarUrls4 avatarUrls { get; set; }
        public string displayName { get; set; }
        public bool active { get; set; }
        public string timeZone { get; set; }
    }

    public class Comment2
    {
        public string self { get; set; }
        public string id { get; set; }
        public Author author { get; set; }
        public string body { get; set; }
        public UpdateAuthor updateAuthor { get; set; }
        public string created { get; set; }
        public string updated { get; set; }
    }

    public class Comment
    {
        public int startAt { get; set; }
        public int maxResults { get; set; }
        public int total { get; set; }
        public List<Comment2> comments { get; set; }
    }

    public class Votes
    {
        public string self { get; set; }
        public int votes { get; set; }
        public bool hasVoted { get; set; }
    }

    public class AvatarUrls5
    {
        public string __invalid_name__48x48 { get; set; }
        public string __invalid_name__24x24 { get; set; }
        public string __invalid_name__16x16 { get; set; }
        public string __invalid_name__32x32 { get; set; }
    }

    public class Assignee
    {
        public string self { get; set; }
        public string name { get; set; }
        public string key { get; set; }
        public string emailAddress { get; set; }
        public AvatarUrls5 avatarUrls { get; set; }
        public string displayName { get; set; }
        public bool active { get; set; }
        public string timeZone { get; set; }
    }

    public class StatusCategory
    {
        public string self { get; set; }
        public int id { get; set; }
        public string key { get; set; }
        public string colorName { get; set; }
        public string name { get; set; }
    }

    public class Status
    {
        public string self { get; set; }
        public string description { get; set; }
        public string iconUrl { get; set; }
        public string name { get; set; }
        public string id { get; set; }
        public StatusCategory statusCategory { get; set; }
    }

    public class Fields
    {
        public string summary { get; set; }
        public Issuetype issuetype { get; set; }
        public List<object> components { get; set; }
        public string created { get; set; }
        public string description { get; set; }
        public Project project { get; set; }
        public Reporter reporter { get; set; }
        public Priority priority { get; set; }
        public object resolution { get; set; }
        public string duedate { get; set; }
        public Comment comment { get; set; }
        public Votes votes { get; set; }
        public Assignee assignee { get; set; }
        public Status status { get; set; }
    }

    public class Issue
    {
        public string expand { get; set; }
        public string id { get; set; }
        public string self { get; set; }
        public string key { get; set; }
        public Fields fields { get; set; }
    }

    public class RootObject
    {
        public string expand { get; set; }
        public int startAt { get; set; }
        public int maxResults { get; set; }
        public int total { get; set; }
        public List<Issue> issues { get; set; }
    }

var t = JsonConvert.DeserializeObject<RootObject>(json);
var issues = t.issues;

5 Comments

but deserializing to strongly typed object in C# is a far better approach.
Accessed JArray values with invalid key value: "GlossDiv". Array position index expected.
Not able to see images. I have modified my JSON to look good.Could you please modify answer as per this.
Its sating "var issues" does not exist in current context during debug. not sure way. My "t" is having result.
issues is just a local variable.How come it doesn't exist in the context. we use var keyword to implicitly declare local variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.