3

I have come across a problem I don't understand when trying to deserialize a JSON string (containing multiple objects) into a List. Here is the JSON.

[  
   {  
      "id":2,
      "name":"Race 1",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   },
   {  
      "id":3,
      "name":"Race 2",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   },
   {  
      "id":4,
      "name":"Race 3",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   },
   {  
      "id":5,
      "name":"Race 4",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   }
]

I then have the JSON parameters matching my Model.

public class RaceModel
{

    public int id { get; set; }
    public string name { get; set; }
    public DateTime raceDateTime { get; set; }
    public DateTime raceStartTime { get; set; }
    public string description { get; set; }
    public int maxEntries { get; set; }
    public int currentEntries { get; set; }
    public int status { get; set; }

}

public class RaceList
{
    public List<RaceList> racelist { get; set; }
}

And my code to get the JSON from the REST API request is below:

string APIServer = Application.Current.Properties["APIServer"].ToString();
string Token = Application.Current.Properties["Token"].ToString();
var client = new RestClient(APIServer);
var request = new RestRequest("api/race", Method.GET);
request.AddHeader("Content-type", "application/json");
request.AddHeader("Authorization", "Bearer " + Token);
var response = client.Execute(request) as RestResponse;

var raceobject = JsonConvert.DeserializeObject<RaceList>(response.Content);

But I get this error (Using Newtonsoft.JSON)

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TechsportiseApp.API.Models.RaceList' 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<T> 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.

I'd like to have a list/collection of objects I can then iterate through and work with.

Can anyone advise what I've done wrong?

dbc
  • 104,963
  • 20
  • 228
  • 340
Matthew Warr
  • 86
  • 1
  • 10
  • 31

1 Answers1

10

Your response is a array of objects and you are specifing a single object in the T parameter. Use List<RaceModel> instead of RaceList:

var raceobject = JsonConvert.DeserializeObject<List<RaceModel>>(response.Content);
L.B
  • 114,136
  • 19
  • 178
  • 224
Einer
  • 505
  • 1
  • 6
  • 15
  • @Matthew Warr Hi I have a similar JSOn and I have JsonConvert.DeserializeObject>(json); but I still get an like the one asked in question. What am I doing wrong. Example contains a List and Item has 5 properties according to JSON. – Abhilash D K Sep 25 '19 at 13:42