skip to Main Content

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


  1. Chosen as BEST ANSWER

    thank you guys, i changed from

    public List<Item> item { get; set; }
    

    to

    public object item { get; set; }
    

    and solve the issue. using dynamic as type also solve the issue.


  2. 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 the Item as an Object rather than a List<Item>. However, you may create a read-only property that may convert the Item Object to List<Item> and finally return List<Item>.

    Login or Signup to reply.
  3. If you use Newtonsoft.Json for serialisation, you can

    a) Define a custom converter as follows:

    public class SingleOrArrayConverter<T> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(List<T>));
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            if (token.Type == JTokenType.Array)
            {
                return token.ToObject<List<T>>();
            }
            return new List<T> { token.ToObject<T>() };
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            List<T> list = (List<T>)value;
            if (list.Count == 1)
            {
                serializer.Serialize(writer, list[0]);
            }
            else
            {
                serializer.Serialize(writer, list);
            }
        }
    }
    

    b) Apply a custom converter to the property of your class:

    public class MaqVeicEquip
    {
        [JsonConverter(typeof(SingleOrArrayConverter<Item>))]
        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; }
        }
    }
    

    c) Deserialise the JSON to an object:

    var str = @"
    {
        ""item"": 
        {
            ""tipoMaq"": ""03"",
            ""marca"": ""DAF"",
            ""idadeMediaFrota"": ""1"",
            ""quantidade"": ""2"",
            ""indOnus"": ""true"",
            ""valorMedio"": ""0.00"",
            ""outrasInformacoes"": ""XF105 FTT510 - BCO PACCAR"",
            ""percPropriedade"": ""100.00""
        }
    }
    ";
    
    var obj = JsonConvert.DeserializeObject<MaqVeicEquip>(str);
    

    Now, the obj should consist of the following data:

    enter image description here

    Hope this helps or at least pushes you in a good direction.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search