Tenho a seguinte dúvida tenho uma classe que contem uma lista e tenho outra classe que lê um JSON a Classe de leitura chama a classe Entidade e monta o objeto mas quando uma das listas vem vazia ela da o seguinte erro.
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Entidades.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.
Alguém sabe como deserializar um JSON mesmo quando há uma lista vazia
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
}]
A lista que esta vindo vazia no caso é a 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;
}
JToken
no caso de JSON.NET) é uma solução rápida. Se a intenção for usar a totalidade, ou a maioria, dos dados, então usar compilação dinamica é uma péssima solução.