Write Json Return to txt c#

Asked

Viewed 439 times

2

I am trying to record a return of a JSON in a txt file. But when I try to use DeserializeObject makes a mistake.

I have the Json below (example of 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 getting him back that way:

IRestResponse response = client.Execute(request);

and try to effect:

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

but the following error occurs:

Cannot deserialize the Current JSON array (e.g. [1,2,3]) into type 'Tabelafipe' 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.

1 answer

3


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

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

Or else:

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

Browser other questions tagged

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