Write Return Json to txt file c #

2

I'm trying to write a return from a JSON into a txt file. But when I try to use DeserializeObject it gives error.

I have Json below (example 2 records):

[
  {
    "TipoVeiculo": "Caminhão",
    "CodigoMarca": 501,
    "Marca": "AGRALE",
    "CodigoModelo": 34,
    "Modelo": "10000 / 10000 S  2p (diesel) (E5)",
    "Ano": 2012,
    "Combustivel": "Diesel",
    "Valor": 96041.00
  },
  {
    "TipoVeiculo": "Caminhão",
    "CodigoMarca": 501,
    "Marca": "AGRALE",
    "CodigoModelo": 34,
    "Modelo": "10000 / 10000 S  2p (diesel) (E5)",
    "Ano": 2013,
    "Combustivel": "Diesel",
    "Valor": 100932.00
  }
]

I'm retrieving it this way:

IRestResponse response = client.Execute(request);

and try to do:

TabelaFipe deserializedProduct = JsonConvert.DeserializeObject<TabelaFipe>(response.Content);

however the following error occurs:

  

Can not deserialize the current JSON array (eg [1,2,3]) into type   'TableFipe' 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 '', line 1, position 1.

    
asked by anonymous 11.07.2016 / 19:55

1 answer

3

If it is a list of TabelaFipe , then you have to use an enumeration, not a simple object, in the type pass:

var deserializedProduct = JsonConvert.DeserializeObject<List<TabelaFipe>>(response.Content);

Or else:

var deserializedProduct = JsonConvert.DeserializeObject<IEnumerable<TabelaFipe>>(response.Content);
    
11.07.2016 / 19:58