I have an object like this
"maqVeicEquip":
{
"item":
{
"tipoMaq": "03",
"marca": "DAF",
"idadeMediaFrota": "1",
"quantidade": "2",
"indOnus": "true",
"valorMedio": "0.00",
"outrasInformacoes": "XF105 FTT510 - BCO PACCAR",
"percPropriedade": "100.00"
}
}
I can’t deserialize it because the item
is a List<>
. You guys know some way to force deserialize item
as a List<Item>
?
Here is my class
public class MaqVeicEquip
{
public List<Item> item { get; set; }
public class Item
{
public string tipoMaq { get; set; }
public string marca { get; set; }
public string idadeMediaFrota { get; set; }
public string quantidade { get; set; }
public string indOnus { get; set; }
public string valorMedio { get; set; }
public string outrasInformacoes { get; set; }
public string percPropriedade { get; set; }
}
}
3
Answers
thank you guys, i changed from
to
and solve the issue. using dynamic as type also solve the issue.
You can not change the rules of deserialization. An object will deserialize to an object & an array/list will deserialize to
List<Item>
. So, I would suggest you declare theItem
as anObject
rather than aList<Item>
. However, you may create a read-only property that may convert theItem
Object
toList<Item>
and finally returnList<Item>
.If you use Newtonsoft.Json for serialisation, you can
a) Define a custom converter as follows:
b) Apply a custom converter to the property of your class:
c) Deserialise the JSON to an object:
Now, the obj should consist of the following data:
Hope this helps or at least pushes you in a good direction.