简体   繁体   中英

Cannot deserialize the current JSON object to a List

I'm trying to deserialize this json:

{
  "veiculo": [
    {
      "categoria": "B",
      "placa": "NLI2892"
    },
    {
      "categoria": "A",
      "placa": "OML8712"
    },
    {
      "categoria": "B",
      "placa": "PQU1492"
    },
    {
      "categoria": "B",
      "placa": "JIC1381"
    },
    {
      "categoria": "A",
      "placa": "ONB1093"
    }
  ]
}

This is my object:

public class VehicleGO
{
    [JsonProperty(PropertyName = "placa")]
    public string Plate { get; set; }

    [JsonProperty(PropertyName = "categoria")]
    public string Category { get; set; }
}

When I try to deserialize:

var vehicles = JsonConvert.DeserializeObject<List<VehicleGO>>(json);

I get the following error:

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[rdscanweb.Models.BaseViewModels.VehicleGO]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object.

Can anyone help me?

This structure should do it.

public class VehicleGO
{
    [JsonProperty(PropertyName = "placa")]
    public string Plate { get; set; }
    [JsonProperty(PropertyName = "categoria")]
    public string Category { get; set; }
}

public class Vehicles
{
    public List<Veiculo> veiculo { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM