I need to deserialize this json using NewtonsoftJson:
{
"players": [
{
"player_id": "331574",
"team_id": "95",
},
{
"player_id": "331575",
"team_id": "95",
}],
"coach":
{
"id": "249197",
"first_name": "Guillermo",
}
}
so I have this class:
public class Squad
{
public List<Player> players { get; set; }
public Coach coach { get; set; }
public class Coach
{
public int id { get; set; }
public string first_name { get; set; }
}
public class Player
{
public int player_id { get; set; }
public int team_id { get; set; }
}
}
for deserialize I use:
return JsonConvert.DeserializeObject<Squad>(content);
now sometimes I get the coach null:
{
"players": [
{
"player_id": "331574",
"team_id": "95",
},
{
"player_id": "331575",
"team_id": "95",
}],
"coach": []
}
So I implemented the converter suggested in this question: How to handle both a single item and an array for the same property using JSON.net
in particular, the only edit I did was:
[JsonConverter(typeof(SingleOrArrayConverter<object>))]
public Coach coach { get; set; }
but I get this error when deserialize:
Can not cast objects of type 'System.Collections.Generic.List`1 [System.Object]' on the 'Coach' type.