C#用一个数组和一个bool值反序列化一个Json字符串

时间:2017-03-28 09:17:36

标签: c# arrays json json.net deserialization

我正在尝试反序列化一个json字符串,但只有bool值附加到我的类,其中数组值始终为null。

            public static EmployeeInformation GetEngineerAdditionInfo(ProjectUserRoles role)
            {
                    EmployeeInformation engineerAdditionalInfo = new EmployeeInformation();
                    var apiBaseUri = string.Empty;
                    apiBaseUri = "https:example.com";
                    Uri newUri = new Uri(apiBaseUri);
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(newUri);
                    httpWebRequest.Method = WebRequestMethods.Http.Get;
                    httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                    using (var response = (HttpWebResponse)httpWebRequest.GetResponse())
                    {
                        using (var reader = new StreamReader(response.GetResponseStream()))
                        {
                            string line = reader.ReadToEnd();

                            engineerAdditionalInfo = JsonConvert.DeserializeObject<EmployeeInformation>(line);
                        }
                     }
              }

我的班级是

    public class EmployeeInformation
    {
        public bool IsSuccess { get; set; }

        List<EmployeeBasicDetails> EmployeeBasicDetails { get; set; }
    }

    public class EmployeeBasicDetails
    {
        public int UserId { get; set; }

        public string EmployeeId { get; set; }

        public string EmailId { get; set; }

        public string EmployeeName { get; set; }
    }

我的Json字符串如下,

{"IsSuccess":true,"EmployeeBasicDetails":[{"UserId":124,"EmployeeId":"SS124","EmailId":"example@example.com","EmployeeName":"Example"},{"UserId":125,"EmployeeId":"SS125","EmailId":"example@example.com","EmployeeName":"Example"},{"UserId":126,"EmployeeId":"SS126","EmailId":"example@example.com","EmployeeName":"Example"},{"UserId":127,"EmployeeId":"SS127","EmailId":"example@example.com","EmployeeName":"Example"}]}

我错过了什么吗?或者有没有其他方法从json字符串中获取数组列表?

提前致谢,

的Dinesh。

1 个答案:

答案 0 :(得分:1)

这只是一个猜测,但我想您忘了将EmployeeBasicDetails设置为公开:

public class EmployeeInformation
{
    public bool IsSuccess { get; set; }

    public List<EmployeeBasicDetails> EmployeeBasicDetails { get; set; }
}

public class EmployeeBasicDetails
{
    public int UserId { get; set; }

    public string EmployeeId { get; set; }

    public string EmailId { get; set; }

    public string EmployeeName { get; set; }
}

希望它有所帮助!