Json with an empty list C#

Asked

Viewed 831 times

0

I have the following doubt I have a class that contains a list and I have another class that reads a JSON the Reading class calls the Entity class and mounts the object but when one of the lists comes empty it gives the following error.

Cannot deserialize the Current JSON array (e.g. [1,2,3]) into type 'Entities.Freightvalue' because the type requires a JSON Object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON Object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that Implements a Collection interface (e.g. Icollection, Ilist) like 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 the case is the 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;
    }
  • Can you put the class definition? We need a MCVE to replicate the problem and help.

  • depending on the bilioteca you are using to deserialize the object, to play on a dynamic object and then do the de-stop is a solution...

  • 1

    @Caiquec. If the intention is to extract 1 or 2 values from JSON, yes, use a dynamic object (or JToken in the case of JSON.NET) is a quick fix. If the intention is to use all or most of the data, then using dynamic compilation is a bad solution.

2 answers

0


The error is here:

public FreightValue freightValue { get; set; }

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

public List<FreightValue> freightValue { get; set; }
  • Thank you so much really solved my problem.

0

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

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

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.