I don't know what I'm doing wrong, I have an extremely simple Json, but I can't deserialize it, I just don't know what else to try. the JSON is this:
{"value":[{"Id":"2b0af0d7-2a7b-4489-a205-8cb9aef88a1b","Code":"1","Name":"Empresa Pripal"},{"Id":"39e415d3-d971-404a-b5ca-fc7f039dd859","Code":"3","Name":"Empresa 3 "},{"Id":"b8405286-a3d1-4d40-9a64-ec8fde498342","Code":"75","Name":"Empresa pruevas"},{"Id":"ec624692-5383-44a8-822a-3493ff177c02","Code":"99","Name":"Empresa 99"}]}
the entity is:
public class Empresas
{
[JsonPropertyName("value")]
IEnumerable<Empresa> value { get; set; }
}
public class Empresa
{
public string Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
And I try to deserialize the Json like so:
Empresas tryObject = JsonSerializer.Deserialize<Empresas>(data);
But tryObject is always null. It is obvious that I am doing something wrong, can you help me please.
Thank you very much.
It's probably because the "value" property of Company must be "public", try changing this
Look at an online example with your same code, but compile and it runs correctly
The code would be like this
Recommendation, you can change the name of the "value" property so as not to use a C# reserved word , but also so that the class is more semantic and not as it is in the JSON (that's why we help with the name attribute) Something like this
Idem for the Company part. So that you do not have Id, Name (but as you design it and help yourself with the name of the JSON field with the JsonPropertyName attribute)
I hope it helps or guides you.
You are trying to deserialize a list of objects as a single object. Try wrapping your model in a list. try this:
List<Empresas> = JsonSerializer.Deserialize<List<Empresas>>(data);
Another thing, put a breakpoint at the moment you do the deserialize and check if data brings something before executing the deserialize. It could be that you are mapping the property wrong. If you can add a screenshot of that please.