[英]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:
但是当我尝试这样做时,为什么对于另一个表它返回具有 null 参数值的对象列表完全相同,代码是:
[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 get 调用正确返回对象,我们可以在这里看到
but when i deserialize the list, list includes null parameters as shown here:但是当我反序列化列表时,列表包括 null 参数,如下所示:
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我只是将所有属性的名称都更改为小写,并且可以正常工作 xD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.