Json with an empty list C #

0

I have the following question I have a class that contains a list and I have another class that reads a JSON class Reading class calls the entity class and assembles the object but when one of the lists comes empty it the following error.

  

Can not deserialize the current JSON array (eg [1,2,3]) into type 'Entities.FreightValue' because the type requires a JSON object (eg {"name": "value"}) to deserialize correctly. / p>      

To fix this error, either change the JSON to a JSON object (eg {"name": "value"}) or change the deserialized type to an array that implements a collection interface List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

     

Path '[0] .freightValue', line 1, position 165.

Does anyone know how to deserialize a JSON even when there is an empty list

JSON:

[{
    "id": "11cc404",
    "slaType": "Normal",
    "name": "teste",
    "scheduledDelivery": false,
    "maxRangeDelivery": 0,
    "dayOfWeekForDelivery": null,
    "dayOfWeekBlockeds": [],
    "freightValue": [],
    "factorCubicWeight": null,
    "freightTableProcessStatus": 1,
    "freightTableValueError": null,
    "modals": [],
    "onlyItemsWithDefinedModal": false,
    "deliveryOnWeekends": false,
    "carrierSchedule": [],
    "maxDimension": {
        "weight": 0.0,
        "height": 0.0,
        "width": 0.0,
        "length": 0.0,
        "maxSumDimension": 0.0
    },
    "exclusiveToDeliveryPoints": false,
    "minimunCubicWeight": 0.0,
    "isPolygon": false,
    "numberOfItemsPerShipment": null
}, {
    "id": "1",
    "slaType": "Normal",
    "name": "Transportadora",
    "scheduledDelivery": false,
    "maxRangeDelivery": 0,
    "dayOfWeekForDelivery": null,
    "dayOfWeekBlockeds": [],
    "freightValue": [],
    "factorCubicWeight": null,
    "freightTableProcessStatus": 1,
    "freightTableValueError": null,
    "modals": [],
    "onlyItemsWithDefinedModal": false,
    "deliveryOnWeekends": false,
    "carrierSchedule": [],
    "maxDimension": {
        "weight": 0.0,
        "height": 0.0,
        "width": 0.0,
        "length": 0.0,
        "maxSumDimension": 0.0
    },
    "exclusiveToDeliveryPoints": false,
    "minimunCubicWeight": 0.0,
    "isPolygon": false,
    "numberOfItemsPerShipment": null
}]

The list that is empty in this case is freightValue .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;


    namespace Entidades
    {
        [DataContract(Name = "RootElement")]
public class ETransportadora
{
    public ETransportadora()
    {
        listaTransportadoras = new List<Transportadora>();
    }
    [DataMember(Name = "Property1")]
    public List<Transportadora> listaTransportadoras { get; set; }
}
[DataContract(Name = "Property1")]
public class Transportadora
{
    [DataMember(Name = "id")]
    public string id { get; set; }
    [DataMember(Name = "slaType")]
    public string slaType { get; set; }
    [DataMember(Name = "name")]
    public string name { get; set; }
    [DataMember(Name = "scheduledDelivery")]
    public bool scheduledDelivery { get; set; }
    [DataMember(Name = "maxRangeDelivery")]
    public int maxRangeDelivery { get; set; }
    [DataMember(Name = "dayOfWeekForDelivery")]
    public object dayOfWeekForDelivery { get; set; }
    [DataMember(Name = "dayOfWeekBlockeds")]
    public object[] dayOfWeekBlockeds { get; set; }
    [DataMember(Name = "freightValue")]
    public FreightValue freightValue { get; set; }
    [DataMember(Name = "factorCubicWeight")]
    public object factorCubicWeight { get; set; }
    [DataMember(Name = "freightTableProcessStatus")]
    public int freightTableProcessStatus { get; set; }
    [DataMember(Name = "freightTableValueError")]
    public object freightTableValueError { get; set; }
    [DataMember(Name = "modals")]
    public object[] modals { get; set; }
    [DataMember(Name = "onlyItemsWithDefinedModal")]
    public bool onlyItemsWithDefinedModal { get; set; }
    [DataMember(Name = "deliveryOnWeekends")]
    public bool deliveryOnWeekends { get; set; }
    [DataMember(Name = "carrierSchedule")]
    public object[] carrierSchedule { get; set; }
    [DataMember(Name = "maxDimension")]
    public Maxdimension maxDimension { get; set; }
    [DataMember(Name = "exclusiveToDeliveryPoints")]
    public bool exclusiveToDeliveryPoints { get; set; }
    [DataMember(Name = "minimunCubicWeight")]
    public float minimunCubicWeight { get; set; }
    [DataMember(Name = "isPolygon")]
    public bool isPolygon { get; set; }
    [DataMember(Name = "numberOfItemsPerShipment")]
    public object numberOfItemsPerShipment { get; set; }
}
[DataContract(Name = "Maxdimension")]
public class Maxdimension
{
    [DataMember(Name = "weight")]
    public float weight { get; set; }
    [DataMember(Name = "height")]
    public float height { get; set; }
    [DataMember(Name = "width")]
    public float width { get; set; }
    [DataMember(Name = "length")]
    public float length { get; set; }
    [DataMember(Name = "maxSumDimension")]
    public float maxSumDimension { get; set; }
}
[DataContract(Name = "FreightValue")]
public class FreightValue
{
    [DataMember(Name = "zipCodeStart")]
    public float zipCodeStart { get; set; }
    [DataMember(Name = "zipCodeEnd")]
    public float zipCodeEnd { get; set; }
    [DataMember(Name = "weightStart")]
    public float weightStart { get; set; }
    [DataMember(Name = "weightEnd")]
    public float weightEnd { get; set; }
    [DataMember(Name = "absoluteMoneyCost")]
    public float absoluteMoneyCost { get; set; }
    [DataMember(Name = "pricePercent")]
    public float pricePercent { get; set; }
    [DataMember(Name = "pricePercentByWeight")]
    public float pricePercentByWeight { get; set; }
    [DataMember(Name = "maxVolume")]
    public float maxVolume { get; set; }
    [DataMember(Name = "timeCost")]
    public float timeCost { get; set; }
    [DataMember(Name = "country")]
    public string country { get; set; }
    [DataMember(Name = "operationType")]
    public int operationType { get; set; }
    [DataMember(Name = "restrictedFreights")]
    public object restrictedFreights { get; set; }
}
}


    private ETransportadora ListarTodasTransportadoras()
    {

        ETransportadora lst = new ETransportadora();
        var results = JsonConvert.DeserializeObject <List<Transportadora>> (content);
        foreach (var transp in results)
        {
            lst.listaTransportadoras.Add(transp);
        }   

        return lst;
    }
    
asked by anonymous 21.08.2015 / 19:47

2 answers

0

The error is here:

public FreightValue freightValue { get; set; }

As you said yourself, freightValue is a list of objects. So, the property also has to be a collection (ex: List<T> )

public List<FreightValue> freightValue { get; set; }
    
21.08.2015 / 22:09
0

Test whether the list with the items is not empty through a conditional (if, for example), and only call the read class if this list is not empty, so you also save application resources.

An empty Json would be an open and closed bracket ("{}")

I hope I have helped

    
21.08.2015 / 20:31