Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

c# - Value from deserialized JSON object is NULL

I am having some trouble getting a value from a deserialized JSON string.

Here is my JSON string:

{
"expand": "names,schema",
"startAt": 0,
"maxResults": 1,
"total": 151,
"issues": [
    {
        "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
        "id": "10210",
        "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/issue/10210",
        "key": "ITSD-202",
        "fields": {
            "statuscategorychangedate": "2021-01-25T20:12:11.922+0000",
            "issuetype": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/issuetype/10003",
                "id": "10003",
                "description": "For customer support issues. Created by Jira Service Desk.",
                "iconUrl": "https://xxxxxxxxxx.atlassian.net/secure/viewavatar?size=medium&avatarId=10308&avatarType=issuetype",
                "name": "Support",
                "subtask": false,
                "avatarId": 10308
            },
            "timespent": null,
            "customfield_10030": null,
            "customfield_10031": {
                "id": "1",
                "name": "Time to resolution",
                "_links": {
                    "self": "https://xxxxxxxxxx.atlassian.net/rest/servicedeskapi/request/10210/sla/1"
                },
                "completedCycles": [],
                "ongoingCycle": {
                    "startTime": {
                        "iso8601": "2021-01-25T20:12:11+0000",
                       "jira": "2021-01-25T20:12:11.592+0000",
                        "friendly": "Yesterday 08:12",
                        "epochMillis": 1611605531592
                    },
                    "breachTime": {
                        "iso8601": "2021-02-11T14:30:00+0000",
                        "jira": "2021-02-11T14:30:00.000+0000",
                        "friendly": "11/Feb/21 02:30",
                        "epochMillis": 1613053800000
                    },
                    "breached": false,
                    "paused": false,
                    "withinCalendarHours": false,
                    "goalDuration": {
                        "millis": 432000000,
                        "friendly": "120h"
                    },
                    "elapsedTime": {
                        "millis": 0,
                        "friendly": "0m"
                    },
                    "remainingTime": {
                        "millis": 432000000,
                        "friendly": "120h"
                    }
                }
            },
            "project": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/project/10000",
                "id": "10000",
                "key": "ITSD",
                "name": "Customer Service Desk",
                "projectTypeKey": "service_desk",
                "simplified": false,
                "avatarUrls": {
                    "48x48": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?pid=10000&avatarId=10412",
                    "24x24": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?size=small&s=small&pid=10000&avatarId=10412",
                    "16x16": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10000&avatarId=10412",
                    "32x32": "https://xxxxxxxxxx.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10000&avatarId=10412"
                }
            },
            "customfield_10032": {
                "id": "2",
                "name": "Time to first response",
                "_links": {
                    "self": "https://xxxxxxxxxx.atlassian.net/rest/servicedeskapi/request/10210/sla/2"
                },
                "completedCycles": [],
                "ongoingCycle": {
                    "startTime": {
                        "iso8601": "2021-01-25T20:12:11+0000",
                        "jira": "2021-01-25T20:12:11.592+0000",
                        "friendly": "Yesterday 08:12",
                        "epochMillis": 1611605531592
                    },
                    "breachTime": {
                        "iso8601": "2021-01-26T12:30:00+0000",
                        "jira": "2021-01-26T12:30:00.000+0000",
                        "friendly": "Today 12:30",
                        "epochMillis": 1611664200000
                    },
                    "breached": false,
                    "paused": false,
                    "withinCalendarHours": false,
                    "goalDuration": {
                        "millis": 14400000,
                        "friendly": "4h"
                    },
                    "elapsedTime": {
                        "millis": 0,
                        "friendly": "0m"
                    },
                    "remainingTime": {
                        "millis": 14400000,
                        "friendly": "4h"
                    }
                }
            },
            "fixVersions": [],
            "customfield_10034": {
                "languageCode": "en",
                "displayName": "English"
            },
            "resolution": null,
            "customfield_10037": null,
            "customfield_10027": null,
            "customfield_10028": null,
            "customfield_10029": null,
            "resolutiondate": null,
            "workratio": -1,
            "lastViewed": "2021-01-25T20:12:11.936+0000",
            "watches": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/issue/ITSD-202/watchers",
                "watchCount": 1,
                "isWatching": true
            },
            "created": "2021-01-25T20:12:11.592+0000",
            "customfield_10020": null,
            "customfield_10021": null,
            "customfield_10022": null,
            "customfield_10023": null,
            "priority": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/priority/3",
                "iconUrl": "https://xxxxxxxxxx.atlassian.net/images/icons/priorities/medium.svg",
                "name": "Medium",
                "id": "3"
            },
            "customfield_10024": null,
            "customfield_10025": null,
            "labels": [],
            "customfield_10026": [],
            "customfield_10016": null,
            "customfield_10017": null,
            "customfield_10018": {
                "hasEpicLinkFieldDependency": false,
                "showField": false,
                "nonEditableReason": {
                    "reason": "PLUGIN_LICENSE_ERROR",
                    "message": "The Parent Link is only available to Jira Premium users."
                }
            },
            "customfield_10019": "0|i0013b:",
            "timeestimate": null,
            "versions": [],
            "assignee": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/user?accountId=12345678912345678912345",
                "accountId": "12345678912345678912345",
                "emailAddress": "[email protected]",
                "avatarUrls": {
                    "48x48": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                    "24x24": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                    "16x16": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png",
                    "32x32": "https://secure.gravatar.com/avatar/2758e009bbe7f2348bfdf52d653b6a2b?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBE-1.png"
                },
                "displayName": "First Last",
                "active": true,
                "timeZone": "Europe/London",
                "accountType": "atlassian"
            },
            "updated": "2021-01-25T20:12:14.321+0000",
            "status": {
                "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/status/10001",
                "description": "This was auto-generated by Jira Service Desk during workflow import",
                "iconUrl": "https://xxxxxxxxxx.atlassian.net/images/icons/status_generic.gif",
                "name": "Waiting for support",
                "id": "10001",
                "statusCategory": {
                    "self": "https://xxxxxxxxxx.atlassian.net/rest/api/3/statuscategory/4",
                    "id": 4,
                    "key": "indeterminate",
                    "colorName": "yellow",
                    "name": "In Progress"
                }
            },
            "components": [],
            "customfield_10050": null,
            "customfield_10051": null,
            "timeoriginalestimate": null,
            "customfield_10052": null,
            "customfield_10053": null,
            "description": null,
            "customfield_10010": null,
            "customfield_10014": null,
            "customfield_10015": null,
            "customfield_10005": null,
            &quo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your class for deserialization should look something 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 int avatarId { get; set; } 
}

public class Links    {
    public string self { get; set; } 
}

public class StartTime    {
    public DateTime iso8601 { get; set; } 
    public DateTime jira { get; set; } 
    public string friendly { get; set; } 
    public long epochMillis { get; set; } 
}

public class BreachTime    {
    public DateTime iso8601 { get; set; } 
    public DateTime jira { get; set; } 
    public string friendly { get; set; } 
    public long epochMillis { get; set; } 
}

public class GoalDuration    {
    public int millis { get; set; } 
    public string friendly { get; set; } 
}

public class ElapsedTime    {
    public int millis { get; set; } 
    public string friendly { get; set; } 
}

public class RemainingTime    {
    public int millis { get; set; } 
    public string friendly { get; set; } 
}

public class OngoingCycle    {
    public StartTime startTime { get; set; } 
    public BreachTime breachTime { get; set; } 
    public bool breached { get; set; } 
    public bool paused { get; set; } 
    public bool withinCalendarHours { get; set; } 
    public GoalDuration goalDuration { get; set; } 
    public ElapsedTime elapsedTime { get; set; } 
    public RemainingTime remainingTime { get; set; } 
}

public class Customfield10031    {
    public string id { get; set; } 
    public string name { get; set; } 
    public Links _links { get; set; } 
    public List<object> completedCycles { get; set; } 
    public OngoingCycle ongoingCycle { get; set; } 
}

public class AvatarUrls    {
    public string _48x48 { get; set; } 
    public string _24x24 { get; set; } 
    public string _16x16 { get; set; } 
    public string _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 string projectTypeKey { get; set; } 
    public bool simplified { get; set; } 
    public AvatarUrls avatarUrls { get; set; } 
}

public class Customfield10032    {
    public string id { get; set; } 
    public string name { get; set; } 
    public Links _links { get; set; } 
    public List<object> completedCycles { get; set; } 
    public OngoingCycle ongoingCycle { get; set; } 
}

public class Customfield10034    {
    public string languageCode { get; set; } 
    public string displayName { get; set; } 
}

public class Watches    {
    public string self { get; set; } 
    public int watchCount { get; set; } 
    public bool isWatching { 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 NonEditableReason    {
    public string reason { get; set; } 
    public string message { get; set; } 
}

public class Customfield10018    {
    public bool hasEpicLinkFieldDependency { get; set; } 
    public bool showField { get; set; } 
    public NonEditableReason nonEditableReason { get; set; } 
}

public class Assignee    {
    public string self { get; set; } 
    public string accountId { get; set; } 
    public string emailAddress { get; set; } 
    public AvatarUrls avatarUrls { get; set; } 
    public string displayName { get; set; } 
    public bool active { get; set; } 
    public string timeZone { get; set; } 
    public string accountType { 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 Creator    {
    public string self { get; set; } 
    public string accountId { get; set; } 
    public string emailAddress { get; set; } 
    public AvatarUrls avatarUrls { get; set; } 
    public string displayName { get; set; } 
    public bool active { get; set; } 
    public string timeZone { get; set; } 
    public string accountType { get; set; } 
}

public class Reporter    {
    public string self { get; set; } 
    public string accountId { get; set; } 
    public string emailAddress { get; set; } 
    public AvatarUrls avatarUrls { get; set; } 
    public string displayName { get; set; } 
    public bool active { get; set; } 
    public string timeZone { get; set; } 
    public string accountType { get; set; } 
}

public class Customfield10002    {
    public string id { get; set; } 
    public string name { get; set; } 
    public Links _links { get; set; } 
}

public class Progress    {
    public int progress { get; set; } 
    public int total { get; set; } 
}

public class Fields    {
    public DateTime statuscategorychangedate { get; set; } 
    public Issuetype issuetype { get; set; } 
    public object timespent { get; set; } 
    public object customfield_10030 { get; set; } 
    public Customfield10031 customfield_10031 { get; set; } 
    public Project project { get; set; } 
    public Customfield10032 customfield_10032 { get; set; } 
    public List<object> fixVersions { get; set; } 
    public Customfield10034 customfield_10034 { get; set; } 
    public object resolution { get; set; } 
    public object customfield_10037 { get; set; } 
    public object customfield_10027 { get; set; } 
    public object customfield_10028 { get; set; } 
    public object customfield_10029 { get; set; } 
    public object resolutiondate { get; set; } 
    public int workratio { get; set; } 
    public DateTime lastViewed { get; set; } 
    public Watches watches { get; set; } 
    public DateTime created { get; set; } 
    public object customfield_10020 { get; set; } 
    public object customfield_10021 { get; set; } 
    public object customfield_10022 { get; set; } 
    public object customfield_10023 { get; set; } 
    public Priority priority { get; set; } 
    public object customfield_10024 { get; set; } 
    public object customfield_10025 { get; set; } 
    public List<object> labels { get; set; } 
    public List<object> customfield_10026 { get; set; } 
    public object customfield_10016 { get; set; } 
    public object customfield_10017 { get; set; } 
    public Customfield10018 customfield_10018 { get; set; } 
    public string customfield_10019 { get; set; } 
    public object timeestimate { get; set; } 
    public List<object> versions { get; set; } 
    public Assignee assignee { get; set; } 
    public DateTime updated { get; set; } 
    public Status status { get; set; } 
    public List<object> components { get; set; } 
    public object customfield_10050 { get; set; } 
    public object customfield_10051 { get; set; } 
    public object timeoriginalestimate { get; set; } 
    public object customfield_10052 { get; set; } 
    public object customfield_10053 { get; set; } 
    public object description { get; set; } 
    public object customfield_10010 { get; set; } 
    public object customfield_10014 { get; set; } 
    public object customfield_10015 { get; set; } 
    public object customfield_10005 { get; set; } 
    public object customfield_10049 { get; set; } 
    public object customfield_10006 { get; set; } 
    public object customfield_10007 { get; set; } 
    public object security { get; set; } 
    public object customfield_10008 { get; set; } 
    public object customfield_10009 { get; set; } 
    public string summary { get; set; } 
    public Creator creator { get; set; } 
    public object customfield_10040 { get; set; } 
    public object customfield_10041 { get; set; } 
    public object customfield_10042 { get; set; } 
    public object customfield_10043 { get; set; } 
    public Reporter reporter { get; set; } 
    public object customfield_10044 { get; set; } 
    public string customfield_10000 { get; set; } 
    public object customfield_10001 { get; set; } 
    public object customfield_10045 { get; set; } 
    public object customfield_10046 { get; set; } 
    public List<Customfield10002> customfield_10002 { get; set; } 
    public object customfield_10003 { get; set; } 
    public object customfield_10047 { get; set; } 
    public object customfield_10048 { get; set; } 
    public object customfield_10004 { get; set; } 
    public object customfield_10038 { get; set; } 
    public object customfield_10039 { get; set; } 
    public object environment { get; set; } 
    public object duedate { get; set; } 
    public Progress progress { 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 Root    {
    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; } 
}

and you'd deserialize like this

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 

Always try to see references when creting json objects, I use this page

As commmented below, this would be the main:

using Newtonsoft.Json;
using System;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Root myDeserializedClass = JsonConvert.DeserializeObject<Root> (File.ReadAllText(@"response.json"));


            foreach (var issue in myDeserializedClass.issues)
            {
                string id = issue.id.ToString();
                string key = issue.key.ToString();
                string summary = issue.fields.summary.ToString();
                string creatoremail = issue.fields.creator.emailAddress.ToString();
            }

        }
    }
}

I think the problem you're having might be the way you're writting your foreach? I mean the code seems to be lacking something, but perhaps you shortened it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...