-2

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 enter image description here

but when i deserialize the list, list includes null parameters as shown here: enter image description 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.

Marco
  • 56,740
  • 14
  • 129
  • 152
  • In my case, I need to add the `[JsonProperty]` attribute; maybe in your case, you need to do the fix shown in [this answert](https://stackoverflow.com/a/66124300/12511801). If you haven't tried yet, check the questions and answers in this search: [https://stackoverflow.com/search?q=JsonSerializer.DeserializeAsync](https://stackoverflow.com/search?q=JsonSerializer.DeserializeAsync). – Marco Aurelio Fernandez Reyes Apr 22 '21 at 21:25
  • Json.NET supports the built-in `DataContract`/`DataMember`/etc` (`using System.Runtime.Serialization`) attributes so please don't use the Json.NET specific attributes unless they provide functionality you can't achieve otherwise. I can't count the hours I've spent converting the latter to allow interoperability outside of that library. – M.Babcock Apr 23 '21 at 00:25

1 Answers1

0

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