简体   繁体   中英

Having a problem with deserialization of an objects

I have a code:

[HttpGet]
        public IEnumerable<Ironman> Get()
        {
            

            var r = baza.Ironman.ToList();
            return r;
        }
private static readonly HttpClient client = new HttpClient();

        private async Task<List<Ironman>> GetIronman()
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var stringKnjige = await client.GetStreamAsync("https://localhost:44369/api/Ironman");
            var knjige = await JsonSerializer.DeserializeAsync<List<Ironman>>(stringKnjige);

            return knjige;
        }
public class Ironman
    {
        [Key]
        public int kljuc { get; set; }
        public string swim { get; set; }
        public string division { get; set; }
        public string run { get; set; }
        public string name { get; set; }
        public string profession { get; set; }
        public string country { get; set; }
        public string age { get; set; }
        public string runDistance { get; set; }
        public string bib { get; set; }
        public string state { get; set; }
        public string bike { get; set; }
        public string genderRank { get; set; }
        public string overall { get; set; }
        public string swimDistance { get; set; }
        public string overallRank { get; set; }
        public string points { get; set; }
        public string t2 { get; set; }
        public string bikeDistance { get; set; }
        public string t1 { get; set; }
        public string divRank { get; set; }


    }

This code above works perfectly fine. It returns objects that I deserialize and databind into datagrid and values are shown perfectly in datagrid. But when i try to do this the exact same why for another table it returns list of objects that have null values for parameters the code is:

[HttpGet]
        public IEnumerable<UltraTriathlon> Get()
        {
            

            var r = baza.UltraTriathlon.ToList();
            return r;
        }
private async Task<List<UltraTriathlon>> GetUltraTriathlon()
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var s = await client.GetStreamAsync("https://localhost:44369/api/UltraTriathlon");
            var k = await JsonSerializer.DeserializeAsync<List<UltraTriathlon>>(s);

            return k;
        }
public class UltraTriathlon
    {
        [Key]
        public int kljuc { get; set; }
        public string Rank { get; set; }
        public string Overall { get; set; }
        public string Competitor { get; set; }
        public string Country { get; set; }
        public string Age_Category { get; set; }
        public string Swim { get; set; }
        public string Trans1 { get; set; }
        public string Bike { get; set; }
        public string Trans2 { get; set; }
        public string Run { get; set; }
        public string Finish { get; set; }
        public string Comment { get; set; }

    }

The get call returns objects correctly as we can see here 在此处输入图像描述

but when i deserialize the list, list includes null parameters as shown here: 在此处输入图像描述

I did bouth methods the same way, and have no clue why the first client method works and fills the list and the second method doesnt.

I just changed names all of my attributes to lower case and it worked xD

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